001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.lang.diagnostics.rendering;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.jet.lang.diagnostics.DiagnosticFactory;
021    import org.jetbrains.jet.lang.diagnostics.Diagnostic;
022    import org.jetbrains.jet.lang.diagnostics.Errors;
023    import org.jetbrains.jet.lang.psi.JetExpression;
024    import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
025    import org.jetbrains.jet.lang.psi.JetTypeConstraint;
026    import org.jetbrains.jet.lang.types.JetType;
027    import org.jetbrains.jet.lexer.JetKeywordToken;
028    import org.jetbrains.jet.renderer.DescriptorRenderer;
029    import org.jetbrains.jet.renderer.Renderer;
030    
031    import java.lang.reflect.Field;
032    import java.lang.reflect.Modifier;
033    import java.util.Collection;
034    
035    import static org.jetbrains.jet.lang.diagnostics.Errors.*;
036    import static org.jetbrains.jet.lang.diagnostics.rendering.Renderers.*;
037    
038    public class DefaultErrorMessages {
039        public static final DiagnosticFactoryToRendererMap MAP = new DiagnosticFactoryToRendererMap();
040        public static final DiagnosticRenderer<Diagnostic> RENDERER = new DispatchingDiagnosticRenderer(MAP);
041    
042        static {
043            MAP.put(EXCEPTION_WHILE_ANALYZING, "{0}", new Renderer<Throwable>() {
044                @NotNull
045                @Override
046                public String render(@NotNull Throwable e) {
047                    return e.getClass().getSimpleName() + ": " + e.getMessage();
048                }
049            });
050    
051            MAP.put(UNRESOLVED_REFERENCE, "Unresolved reference: {0}", ELEMENT_TEXT);
052    
053            MAP.put(INVISIBLE_REFERENCE, "Cannot access ''{0}'': it is ''{1}'' in ''{2}''", NAME, TO_STRING, NAME);
054            MAP.put(INVISIBLE_MEMBER, "Cannot access ''{0}'': it is ''{1}'' in ''{2}''", NAME, TO_STRING, NAME);
055    
056            MAP.put(REDECLARATION, "Redeclaration: {0}", NAME);
057            MAP.put(NAME_SHADOWING, "Name shadowed: {0}", NAME);
058    
059            MAP.put(TYPE_MISMATCH, "Type mismatch: inferred type is {1} but {0} was expected", RENDER_TYPE, RENDER_TYPE);
060            MAP.put(INCOMPATIBLE_MODIFIERS, "Incompatible modifiers: ''{0}''", new Renderer<Collection<JetKeywordToken>>() {
061                @NotNull
062                @Override
063                public String render(@NotNull Collection<JetKeywordToken> tokens) {
064                    StringBuilder sb = new StringBuilder();
065                    for (JetKeywordToken token : tokens) {
066                        if (sb.length() != 0) {
067                            sb.append(" ");
068                        }
069                        sb.append(token.getValue());
070                    }
071                    return sb.toString();
072                }
073            });
074            MAP.put(ILLEGAL_MODIFIER, "Illegal modifier ''{0}''", TO_STRING);
075    
076            MAP.put(REDUNDANT_MODIFIER, "Modifier ''{0}'' is redundant because ''{1}'' is present", TO_STRING, TO_STRING);
077            MAP.put(ABSTRACT_MODIFIER_IN_TRAIT, "Modifier ''abstract'' is redundant in trait");
078            MAP.put(OPEN_MODIFIER_IN_TRAIT, "Modifier ''open'' is redundant in trait");
079            MAP.put(OPEN_MODIFIER_IN_ENUM, "Modifier ''open'' is not applicable for enum class");
080            MAP.put(ILLEGAL_ENUM_ANNOTATION, "Annotation ''enum'' is only applicable for class");
081            MAP.put(REDUNDANT_MODIFIER_IN_GETTER, "Visibility modifiers are redundant in getter");
082            MAP.put(TRAIT_CAN_NOT_BE_FINAL, "Trait cannot be final");
083            MAP.put(TYPECHECKER_HAS_RUN_INTO_RECURSIVE_PROBLEM,
084                    "Type checking has run into a recursive problem. Easiest workaround: specify types of your declarations explicitly"); // TODO: message
085            MAP.put(RETURN_NOT_ALLOWED, "'return' is not allowed here");
086            MAP.put(RETURN_NOT_ALLOWED_EXPLICIT_RETURN_TYPE_REQUIRED, "'return' is only allowed in function literals that have return types specified explicitly");
087            MAP.put(PROJECTION_IN_IMMEDIATE_ARGUMENT_TO_SUPERTYPE, "Projections are not allowed for immediate arguments of a supertype");
088            MAP.put(LABEL_NAME_CLASH, "There is more than one label with such a name in this scope");
089            MAP.put(EXPRESSION_EXPECTED_NAMESPACE_FOUND, "Expression expected, but a namespace name found");
090    
091            MAP.put(CANNOT_IMPORT_FROM_ELEMENT, "Cannot import from ''{0}''", NAME);
092            MAP.put(CANNOT_BE_IMPORTED, "Cannot import ''{0}'', functions and properties can be imported only from packages", NAME);
093            MAP.put(USELESS_HIDDEN_IMPORT, "Useless import, it is hidden further");
094            MAP.put(USELESS_SIMPLE_IMPORT, "Useless import, does nothing");
095            MAP.put(PLATFORM_CLASS_MAPPED_TO_KOTLIN, "This class shouldn''t be used in Kotlin. Use {0} instead.", CLASSES_OR_SEPARATED);
096    
097            MAP.put(CANNOT_INFER_PARAMETER_TYPE,
098                    "Cannot infer a type for this parameter. To specify it explicitly use the {(p : Type) => ...} notation");
099    
100            MAP.put(NO_BACKING_FIELD_ABSTRACT_PROPERTY, "This property doesn't have a backing field, because it's abstract");
101            MAP.put(NO_BACKING_FIELD_CUSTOM_ACCESSORS,
102                    "This property doesn't have a backing field, because it has custom accessors without reference to the backing field");
103            MAP.put(INACCESSIBLE_BACKING_FIELD, "The backing field is not accessible here");
104            MAP.put(NOT_PROPERTY_BACKING_FIELD, "The referenced variable is not a property and doesn't have backing field");
105    
106            MAP.put(MIXING_NAMED_AND_POSITIONED_ARGUMENTS, "Mixing named and positioned arguments in not allowed");
107            MAP.put(ARGUMENT_PASSED_TWICE, "An argument is already passed for this parameter");
108            MAP.put(NAMED_PARAMETER_NOT_FOUND, "Cannot find a parameter with this name: {0}", ELEMENT_TEXT);
109            MAP.put(VARARG_OUTSIDE_PARENTHESES, "Passing value as a vararg is only allowed inside a parenthesized argument list");
110            MAP.put(NON_VARARG_SPREAD, "The spread operator (*foo) may only be applied in a vararg position");
111    
112            MAP.put(MANY_FUNCTION_LITERAL_ARGUMENTS, "Only one function literal is allowed outside a parenthesized argument list");
113            MAP.put(PROPERTY_WITH_NO_TYPE_NO_INITIALIZER, "This property must either have a type annotation, be initialized or be delegated");
114            MAP.put(VARIABLE_WITH_NO_TYPE_NO_INITIALIZER, "This variable must either have a type annotation or be initialized");
115    
116            MAP.put(INITIALIZER_REQUIRED_FOR_MULTIDECLARATION, "Initializer required for multi-declaration");
117            MAP.put(COMPONENT_FUNCTION_MISSING, "Multi-declaration initializer of type {1} must have a ''{0}()'' function", TO_STRING, RENDER_TYPE);
118            MAP.put(COMPONENT_FUNCTION_AMBIGUITY, "Function ''{0}()'' is ambiguous for this expression: {1}", TO_STRING, AMBIGUOUS_CALLS);
119            MAP.put(COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH, "''{0}()'' function returns ''{1}'', but ''{2}'' is expected",
120                                                                                        TO_STRING, RENDER_TYPE, RENDER_TYPE);
121    
122            MAP.put(ABSTRACT_PROPERTY_IN_PRIMARY_CONSTRUCTOR_PARAMETERS, "This property cannot be declared abstract");
123            MAP.put(ABSTRACT_PROPERTY_WITH_INITIALIZER, "Property with initializer cannot be abstract");
124            MAP.put(ABSTRACT_PROPERTY_WITH_GETTER, "Property with getter implementation cannot be abstract");
125            MAP.put(ABSTRACT_PROPERTY_WITH_SETTER, "Property with setter implementation cannot be abstract");
126    
127            MAP.put(ABSTRACT_DELEGATED_PROPERTY, "Delegated property cannot be abstract");
128            MAP.put(ACCESSOR_FOR_DELEGATED_PROPERTY, "Delegated property cannot have accessor");
129            MAP.put(DELEGATED_PROPERTY_IN_TRAIT, "Delegated properties are not allowed in traits");
130            MAP.put(LOCAL_VARIABLE_WITH_DELEGATE, "Local variables are not allowed to have delegates");
131    
132            MAP.put(PACKAGE_MEMBER_CANNOT_BE_PROTECTED, "Package member cannot be protected");
133    
134            MAP.put(GETTER_VISIBILITY_DIFFERS_FROM_PROPERTY_VISIBILITY, "Getter visibility must be the same as property visibility");
135            MAP.put(BACKING_FIELD_IN_TRAIT, "Property in a trait cannot have a backing field");
136            MAP.put(MUST_BE_INITIALIZED, "Property must be initialized");
137            MAP.put(MUST_BE_INITIALIZED_OR_BE_ABSTRACT, "Property must be initialized or be abstract");
138            MAP.put(PROPERTY_INITIALIZER_IN_TRAIT, "Property initializers are not allowed in traits");
139            MAP.put(FINAL_PROPERTY_IN_TRAIT, "Abstract property in trait cannot be final");
140            MAP.put(PROPERTY_INITIALIZER_NO_BACKING_FIELD, "Initializer is not allowed here because this property has no backing field");
141            MAP.put(ABSTRACT_PROPERTY_IN_NON_ABSTRACT_CLASS, "Abstract property ''{0}'' in non-abstract class ''{1}''", NAME, NAME);
142            MAP.put(ABSTRACT_FUNCTION_IN_NON_ABSTRACT_CLASS, "Abstract function ''{0}'' in non-abstract class ''{1}''", NAME, NAME);
143            MAP.put(ABSTRACT_FUNCTION_WITH_BODY, "A function ''{0}'' with body cannot be abstract", NAME);
144            MAP.put(NON_ABSTRACT_FUNCTION_WITH_NO_BODY, "Function ''{0}'' without a body must be abstract", NAME);
145            MAP.put(FINAL_FUNCTION_WITH_NO_BODY, "Function ''{0}'' without body cannot be final", NAME);
146    
147            MAP.put(NON_MEMBER_FUNCTION_NO_BODY, "Function ''{0}'' must have a body", NAME);
148            MAP.put(NON_FINAL_MEMBER_IN_FINAL_CLASS, "\"open\" has no effect in a final class");
149    
150            MAP.put(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE, "Public or protected member should have specified type");
151    
152            MAP.put(PROJECTION_ON_NON_CLASS_TYPE_ARGUMENT, "Projections are not allowed on type arguments of functions and properties");
153            MAP.put(SUPERTYPE_NOT_INITIALIZED, "This type has a constructor, and thus must be initialized here");
154            MAP.put(NOTHING_TO_OVERRIDE, "''{0}'' overrides nothing", NAME);
155            MAP.put(VIRTUAL_MEMBER_HIDDEN, "''{0}'' hides member of supertype ''{2}'' and needs ''override'' modifier", NAME, NAME, NAME);
156    
157            MAP.put(DATA_CLASS_OVERRIDE_CONFLICT, "Function ''{0}'' generated for the data class conflicts with member of supertype ''{1}''", NAME, NAME);
158    
159            MAP.put(CANNOT_OVERRIDE_INVISIBLE_MEMBER, "''{0}'' cannot has no access to ''{1}'' in class {2}, so it cannot override it",
160                    DescriptorRenderer.TEXT, DescriptorRenderer.TEXT, DescriptorRenderer.TEXT);
161            MAP.put(CANNOT_INFER_VISIBILITY, "Cannot infer visibility. Please specify it explicitly");
162    
163            MAP.put(ENUM_ENTRY_SHOULD_BE_INITIALIZED, "Missing delegation specifier ''{0}''", NAME);
164            MAP.put(ENUM_ENTRY_ILLEGAL_TYPE, "The type constructor of enum entry should be ''{0}''", NAME);
165    
166            MAP.put(UNINITIALIZED_VARIABLE, "Variable ''{0}'' must be initialized", NAME);
167            MAP.put(UNINITIALIZED_PARAMETER, "Parameter ''{0}'' is uninitialized here", NAME);
168            MAP.put(UNUSED_VARIABLE, "Variable ''{0}'' is never used", NAME);
169            MAP.put(UNUSED_PARAMETER, "Parameter ''{0}'' is never used", NAME);
170            MAP.put(ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE, "Variable ''{0}'' is assigned but never accessed", NAME);
171            MAP.put(VARIABLE_WITH_REDUNDANT_INITIALIZER, "Variable ''{0}'' initializer is redundant", NAME);
172            MAP.put(UNUSED_VALUE, "The value ''{0}'' assigned to ''{1}'' is never used", ELEMENT_TEXT, TO_STRING);
173            MAP.put(UNUSED_CHANGED_VALUE, "The value changed at ''{0}'' is never used", ELEMENT_TEXT);
174            MAP.put(UNUSED_EXPRESSION, "The expression is unused");
175            MAP.put(UNUSED_FUNCTION_LITERAL, "The function literal is unused. If you mean block, you can use 'run { ... }'");
176    
177            MAP.put(VAL_REASSIGNMENT, "Val cannot be reassigned", NAME);
178            MAP.put(INVISIBLE_SETTER, "Cannot assign to ''{0}'': the setter is ''{1}'' in ''{2}''", NAME, TO_STRING, NAME);
179            MAP.put(INITIALIZATION_BEFORE_DECLARATION, "Variable cannot be initialized before declaration", NAME);
180            MAP.put(VARIABLE_EXPECTED, "Variable expected");
181    
182            MAP.put(VAL_OR_VAR_ON_LOOP_PARAMETER, "''{0}'' on loop parameter is not allowed", TO_STRING);
183            MAP.put(VAL_OR_VAR_ON_FUN_PARAMETER, "''{0}'' on function parameter is not allowed", TO_STRING);
184            MAP.put(VAL_OR_VAR_ON_CATCH_PARAMETER, "''{0}'' on catch parameter is not allowed", TO_STRING);
185    
186            MAP.put(INITIALIZATION_USING_BACKING_FIELD_CUSTOM_SETTER,
187                    "This property has a custom setter, so initialization using backing field required", NAME);
188            MAP.put(INITIALIZATION_USING_BACKING_FIELD_OPEN_SETTER,
189                    "Setter of this property can be overridden, so initialization using backing field required", NAME);
190    
191            MAP.put(UNREACHABLE_CODE, "Unreachable code");
192    
193            MAP.put(MANY_CLASS_OBJECTS, "Only one class object is allowed per class");
194            MAP.put(CLASS_OBJECT_NOT_ALLOWED, "A class object is not allowed here");
195            MAP.put(DELEGATION_IN_TRAIT, "Traits cannot use delegation");
196            MAP.put(DELEGATION_NOT_TO_TRAIT, "Only traits can be delegated to");
197            MAP.put(NO_CONSTRUCTOR, "This class does not have a constructor");
198            MAP.put(NOT_A_CLASS, "Not a class");
199            MAP.put(ILLEGAL_ESCAPE_SEQUENCE, "Illegal escape sequence");
200    
201            MAP.put(LOCAL_EXTENSION_PROPERTY, "Local extension properties are not allowed");
202            MAP.put(LOCAL_VARIABLE_WITH_GETTER, "Local variables are not allowed to have getters");
203            MAP.put(LOCAL_VARIABLE_WITH_SETTER, "Local variables are not allowed to have setters");
204            MAP.put(VAL_WITH_SETTER, "A 'val'-property cannot have a setter");
205    
206            MAP.put(NO_GET_METHOD, "No get method providing array access");
207            MAP.put(NO_SET_METHOD, "No set method providing array access");
208    
209            MAP.put(INC_DEC_SHOULD_NOT_RETURN_UNIT, "Functions inc(), dec() shouldn't return Unit to be used by operators ++, --");
210            MAP.put(ASSIGNMENT_OPERATOR_SHOULD_RETURN_UNIT, "Function ''{0}'' should return Unit to be used by corresponding operator ''{1}''",
211                    NAME, ELEMENT_TEXT);
212            MAP.put(ASSIGN_OPERATOR_AMBIGUITY, "Assignment operators ambiguity: {0}", AMBIGUOUS_CALLS);
213    
214            MAP.put(EQUALS_MISSING, "No method 'equals(jet.Any?) : jet.Boolean' available");
215            MAP.put(ASSIGNMENT_IN_EXPRESSION_CONTEXT, "Assignments are not expressions, and only expressions are allowed in this context");
216            MAP.put(NAMESPACE_IS_NOT_AN_EXPRESSION, "'namespace' is not an expression, it can only be used on the left-hand side of a dot ('.')");
217            MAP.put(SUPER_IS_NOT_AN_EXPRESSION, "''{0}'' is not an expression, it can only be used on the left-hand side of a dot ('.')", TO_STRING);
218            MAP.put(DECLARATION_IN_ILLEGAL_CONTEXT, "Declarations are not allowed in this position");
219            MAP.put(SETTER_PARAMETER_WITH_DEFAULT_VALUE, "Setter parameters cannot have default values");
220            MAP.put(NO_THIS, "'this' is not defined in this context");
221            MAP.put(SUPER_NOT_AVAILABLE, "No supertypes are accessible in this context");
222            MAP.put(SUPERCLASS_NOT_ACCESSIBLE_FROM_TRAIT, "Superclass is not accessible from trait");
223            MAP.put(AMBIGUOUS_SUPER, "Many supertypes available, please specify the one you mean in angle brackets, e.g. 'super<Foo>'");
224            MAP.put(ABSTRACT_SUPER_CALL, "Abstract member cannot be accessed directly");
225            MAP.put(NOT_A_SUPERTYPE, "Not a supertype");
226            MAP.put(TYPE_ARGUMENTS_REDUNDANT_IN_SUPER_QUALIFIER, "Type arguments do not need to be specified in a 'super' qualifier");
227            MAP.put(USELESS_CAST_STATIC_ASSERT_IS_FINE, "No cast needed, use ':' instead");
228            MAP.put(USELESS_CAST, "No cast needed");
229            MAP.put(CAST_NEVER_SUCCEEDS, "This cast can never succeed");
230            MAP.put(USELESS_NULLABLE_CHECK, "Non-null type is checked for instance of nullable type");
231            MAP.put(WRONG_SETTER_PARAMETER_TYPE, "Setter parameter type must be equal to the type of the property, i.e. ''{0}''", RENDER_TYPE, RENDER_TYPE);
232            MAP.put(WRONG_GETTER_RETURN_TYPE, "Getter return type must be equal to the type of the property, i.e. ''{0}''", RENDER_TYPE, RENDER_TYPE);
233            MAP.put(NO_CLASS_OBJECT, "Please specify constructor invocation; classifier ''{0}'' does not have a class object", NAME);
234            MAP.put(TYPE_PARAMETER_IS_NOT_AN_EXPRESSION, "Type parameter ''{0}'' is not an expression", NAME);
235            MAP.put(TYPE_PARAMETER_ON_LHS_OF_DOT, "Type parameter ''{0}'' cannot have or inherit a class object, so it cannot be on the left hand side of dot", NAME);
236            MAP.put(NO_GENERICS_IN_SUPERTYPE_SPECIFIER, "Generic arguments of the base type must be specified");
237    
238            MAP.put(INACCESSIBLE_OUTER_CLASS_EXPRESSION, "Expression is inaccessible from a nested class ''{0}'', use ''inner'' keyword to make the class inner", NAME);
239            MAP.put(NESTED_CLASS_NOT_ALLOWED, "Nested class is not allowed here, use ''inner'' keyword to make the class inner");
240    
241            MAP.put(HAS_NEXT_MISSING, "hasNext() cannot be called on iterator() of type ''{0}''", RENDER_TYPE);
242            MAP.put(HAS_NEXT_FUNCTION_AMBIGUITY, "hasNext() is ambiguous for iterator() of type ''{0}''", RENDER_TYPE);
243            MAP.put(HAS_NEXT_FUNCTION_NONE_APPLICABLE, "None of the hasNext() functions is applicable for iterator() of type ''{0}''", RENDER_TYPE);
244            MAP.put(HAS_NEXT_FUNCTION_TYPE_MISMATCH, "The ''iterator().hasNext()'' function of the loop range must return jet.Boolean, but returns {0}",
245                    RENDER_TYPE);
246    
247            MAP.put(NEXT_MISSING, "next() cannot be called on iterator() of type ''{0}''", RENDER_TYPE);
248            MAP.put(NEXT_AMBIGUITY, "next() is ambiguous for iterator() of type ''{0}''", RENDER_TYPE);
249            MAP.put(NEXT_NONE_APPLICABLE, "None of the next() functions is applicable for iterator() of type ''{0}''", RENDER_TYPE);
250    
251            MAP.put(ITERATOR_MISSING, "For-loop range must have an iterator() method");
252            MAP.put(ITERATOR_AMBIGUITY, "Method ''iterator()'' is ambiguous for this expression: {0}", AMBIGUOUS_CALLS);
253    
254            MAP.put(DELEGATE_SPECIAL_FUNCTION_MISSING, "Missing ''{0}'' method on delegate of type ''{1}''", TO_STRING, RENDER_TYPE);
255            MAP.put(DELEGATE_SPECIAL_FUNCTION_AMBIGUITY, "Overload resolution ambiguity on method ''{0}'': {1}",  TO_STRING, AMBIGUOUS_CALLS);
256            MAP.put(DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE, "Property delegate must have a ''{0}'' method. None of the following functions is suitable: {1}", TO_STRING,  AMBIGUOUS_CALLS);
257            MAP.put(DELEGATE_SPECIAL_FUNCTION_RETURN_TYPE_MISMATCH, "The ''{0}'' function of property delegate is expected to return ''{1}'', but returns ''{2}''",
258                    TO_STRING, RENDER_TYPE, RENDER_TYPE);
259    
260            MAP.put(COMPARE_TO_TYPE_MISMATCH, "''compareTo()'' must return jet.Int, but returns {0}", RENDER_TYPE);
261            MAP.put(CALLEE_NOT_A_FUNCTION, "Expecting a function type, but found {0}", RENDER_TYPE);
262    
263            MAP.put(RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY,
264                    "Returns are not allowed for functions with expression body. Use block body in '{...}'");
265            MAP.put(NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY, "A 'return' expression required in a function with a block body ('{...}')");
266            MAP.put(RETURN_TYPE_MISMATCH, "This function must return a value of type {0}", RENDER_TYPE);
267            MAP.put(EXPECTED_TYPE_MISMATCH, "Expected a value of type {0}", RENDER_TYPE);
268            MAP.put(ASSIGNMENT_TYPE_MISMATCH,
269                    "Expected a value of type {0}. Assignment operation is not an expression, so it does not return any value", RENDER_TYPE);
270    
271            MAP.put(EXPECTED_PARAMETER_TYPE_MISMATCH, "Expected parameter of type {0}", RENDER_TYPE);
272            MAP.put(EXPECTED_RETURN_TYPE_MISMATCH, "Expected return type {0}", RENDER_TYPE);
273            MAP.put(EXPECTED_PARAMETERS_NUMBER_MISMATCH, "Expected {0,choice,0#no parameters|1#one parameter of type|1<{0,number,integer} parameters of types} {1}", null, RENDER_COLLECTION_OF_TYPES);
274    
275            MAP.put(IMPLICIT_CAST_TO_UNIT_OR_ANY, "Type was casted to ''{0}''. Please specify ''{0}'' as expected type, if you mean such cast",
276                    RENDER_TYPE);
277            MAP.put(EXPRESSION_EXPECTED, "{0} is not an expression, and only expressions are allowed here", new Renderer<JetExpression>() {
278                @NotNull
279                @Override
280                public String render(@NotNull JetExpression expression) {
281                    String expressionType = expression.toString();
282                    return expressionType.substring(0, 1) +
283                           expressionType.substring(1).toLowerCase();
284                }
285            });
286    
287            MAP.put(UPPER_BOUND_VIOLATED, "Type argument is not within its bounds: should be subtype of ''{0}''", RENDER_TYPE, RENDER_TYPE);
288            MAP.put(FINAL_CLASS_OBJECT_UPPER_BOUND, "''{0}'' is a final type, and thus a class object cannot extend it", RENDER_TYPE);
289            MAP.put(FINAL_UPPER_BOUND, "''{0}'' is a final type, and thus a value of the type parameter is predetermined", RENDER_TYPE);
290            MAP.put(USELESS_ELVIS, "Elvis operator (?:) always returns the left operand of non-nullable type {0}", RENDER_TYPE);
291            MAP.put(CONFLICTING_UPPER_BOUNDS, "Upper bounds of {0} have empty intersection", NAME);
292            MAP.put(CONFLICTING_CLASS_OBJECT_UPPER_BOUNDS, "Class object upper bounds of {0} have empty intersection", NAME);
293    
294            MAP.put(TOO_MANY_ARGUMENTS, "Too many arguments for {0}", DescriptorRenderer.TEXT);
295    
296            MAP.put(CONSTANT_EXPECTED_TYPE_MISMATCH, "An {0} literal does not conform to the expected type {1}", TO_STRING, RENDER_TYPE);
297            MAP.put(INT_LITERAL_OUT_OF_RANGE, "The value is out of range");
298            MAP.put(FLOAT_LITERAL_OUT_OF_RANGE, "The value is out of range");
299            MAP.put(INCORRECT_CHARACTER_LITERAL, "Incorrect character literal");
300            MAP.put(EMPTY_CHARACTER_LITERAL, "Empty character literal");
301            MAP.put(TOO_MANY_CHARACTERS_IN_CHARACTER_LITERAL, "Too many characters in a character literal ''{0}''", ELEMENT_TEXT);
302            MAP.put(ILLEGAL_ESCAPE, "Illegal escape: ''{0}''", ELEMENT_TEXT);
303            MAP.put(NULL_FOR_NONNULL_TYPE, "Null can not be a value of a non-null type {0}", RENDER_TYPE);
304    
305            MAP.put(ELSE_MISPLACED_IN_WHEN, "'else' entry must be the last one in a when-expression");
306    
307            MAP.put(NO_ELSE_IN_WHEN, "'when' expression must contain 'else' branch");
308            MAP.put(TYPE_MISMATCH_IN_RANGE, "Type mismatch: incompatible types of range and element checked in it");
309            MAP.put(CYCLIC_INHERITANCE_HIERARCHY, "There's a cycle in the inheritance hierarchy for this type");
310    
311            MAP.put(MANY_CLASSES_IN_SUPERTYPE_LIST, "Only one class may appear in a supertype list");
312            MAP.put(SUPERTYPE_NOT_A_CLASS_OR_TRAIT, "Only classes and traits may serve as supertypes");
313            MAP.put(SUPERTYPE_INITIALIZED_IN_TRAIT, "Traits cannot initialize supertypes");
314            MAP.put(CLASS_IN_SUPERTYPE_FOR_ENUM, "Enum class cannot inherit from classes");
315            MAP.put(CONSTRUCTOR_IN_TRAIT, "A trait may not have a constructor");
316            MAP.put(SUPERTYPE_APPEARS_TWICE, "A supertype appears twice");
317            MAP.put(FINAL_SUPERTYPE, "This type is final, so it cannot be inherited from");
318    
319            MAP.put(ILLEGAL_SELECTOR, "Expression ''{0}'' cannot be a selector (occur after a dot)", TO_STRING);
320    
321            MAP.put(VALUE_PARAMETER_WITH_NO_TYPE_ANNOTATION, "A type annotation is required on a value parameter");
322            MAP.put(BREAK_OR_CONTINUE_OUTSIDE_A_LOOP, "'break' and 'continue' are only allowed inside a loop");
323            MAP.put(NOT_A_LOOP_LABEL, "The label ''{0}'' does not denote a loop", TO_STRING);
324            MAP.put(NOT_A_RETURN_LABEL, "The label ''{0}'' does not reference to a context from which we can return", TO_STRING);
325    
326            MAP.put(ANONYMOUS_INITIALIZER_IN_TRAIT, "Anonymous initializers are not allowed in traits");
327            MAP.put(NULLABLE_SUPERTYPE, "A supertype cannot be nullable");
328            MAP.put(REDUNDANT_NULLABLE, "Redundant '?'");
329            MAP.put(BASE_WITH_NULLABLE_UPPER_BOUND, "''{0}'' has a nullable upper bound. " +
330                                                    "This means that a value of this type may be null. " +
331                                                    "Using ''{0}?'' is likely to mislead the reader", RENDER_TYPE);
332            MAP.put(UNSAFE_CALL, "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type {0}", RENDER_TYPE);
333            MAP.put(AMBIGUOUS_LABEL, "Ambiguous label");
334            MAP.put(UNSUPPORTED, "Unsupported [{0}]", TO_STRING);
335            MAP.put(UNNECESSARY_SAFE_CALL, "Unnecessary safe call on a non-null receiver of type {0}", RENDER_TYPE);
336            MAP.put(UNNECESSARY_NOT_NULL_ASSERTION, "Unnecessary non-null assertion (!!) on a non-null receiver of type {0}", RENDER_TYPE);
337            MAP.put(NAME_IN_CONSTRAINT_IS_NOT_A_TYPE_PARAMETER, "{0} does not refer to a type parameter of {1}", new Renderer<JetTypeConstraint>() {
338                @NotNull
339                @Override
340                public String render(@NotNull JetTypeConstraint typeConstraint) {
341                    //noinspection ConstantConditions
342                    return typeConstraint.getSubjectTypeParameterName().getReferencedName();
343                }
344            }, NAME);
345            MAP.put(AUTOCAST_IMPOSSIBLE, "Automatic cast to ''{0}'' is impossible, because ''{1}'' could have changed since the is-check", RENDER_TYPE,
346                    NAME);
347    
348            MAP.put(VARIANCE_ON_TYPE_PARAMETER_OF_FUNCTION_OR_PROPERTY, "Variance annotations are only allowed for type parameters of classes and traits");
349            MAP.put(REDUNDANT_PROJECTION, "Projection is redundant: the corresponding type parameter of {0} has the same variance", NAME);
350            MAP.put(CONFLICTING_PROJECTION, "Projection is conflicting with variance of the corresponding type parameter of {0}. Remove the projection or replace it with ''*''", NAME);
351    
352            MAP.put(TYPE_MISMATCH_IN_FOR_LOOP, "The loop iterates over values of type {0} but the parameter is declared to be {1}", RENDER_TYPE,
353                    RENDER_TYPE);
354            MAP.put(TYPE_MISMATCH_IN_CONDITION, "Condition must be of type jet.Boolean, but is of type {0}", RENDER_TYPE);
355            MAP.put(INCOMPATIBLE_TYPES, "Incompatible types: {0} and {1}", RENDER_TYPE, RENDER_TYPE);
356            MAP.put(EXPECTED_CONDITION, "Expected condition of jet.Boolean type");
357    
358            MAP.put(CANNOT_CHECK_FOR_ERASED, "Cannot check for instance of erased type: {0}", RENDER_TYPE);
359            MAP.put(UNCHECKED_CAST, "Unchecked cast: {0} to {1}", RENDER_TYPE, RENDER_TYPE);
360    
361            MAP.put(INCONSISTENT_TYPE_PARAMETER_VALUES, "Type parameter {0} of ''{1}'' has inconsistent values: {2}", NAME, NAME, RENDER_COLLECTION_OF_TYPES);
362    
363            MAP.put(EQUALITY_NOT_APPLICABLE, "Operator ''{0}'' cannot be applied to ''{1}'' and ''{2}''", new Renderer<JetSimpleNameExpression>() {
364                @NotNull
365                @Override
366                public String render(@NotNull JetSimpleNameExpression nameExpression) {
367                    //noinspection ConstantConditions
368                    return nameExpression.getReferencedName();
369                }
370            }, RENDER_TYPE, RENDER_TYPE);
371    
372            MAP.put(SENSELESS_COMPARISON, "Condition ''{0}'' is always ''{1}''", ELEMENT_TEXT, TO_STRING);
373            MAP.put(SENSELESS_NULL_IN_WHEN, "Expression under 'when' is never equal to null");
374    
375            MAP.put(OVERRIDING_FINAL_MEMBER, "''{0}'' in ''{1}'' is final and cannot be overridden", NAME, NAME);
376            MAP.put(CANNOT_WEAKEN_ACCESS_PRIVILEGE, "Cannot weaken access privilege ''{0}'' for ''{1}'' in ''{2}''", TO_STRING, NAME, NAME);
377            MAP.put(CANNOT_CHANGE_ACCESS_PRIVILEGE, "Cannot change access privilege ''{0}'' for ''{1}'' in ''{2}''", TO_STRING, NAME, NAME);
378    
379            MAP.put(RETURN_TYPE_MISMATCH_ON_OVERRIDE, "Return type of ''{0}'' is not a subtype of the return type of overridden member {1}",
380                    NAME, DescriptorRenderer.TEXT);
381    
382            MAP.put(PROPERTY_TYPE_MISMATCH_ON_OVERRIDE, "Type of ''{0}'' doesn't match to the type of overridden var-property {1}",
383                    NAME, DescriptorRenderer.TEXT);
384    
385            MAP.put(VAR_OVERRIDDEN_BY_VAL, "Var-property {0} cannot be overridden by val-property {1}", DescriptorRenderer.TEXT,
386                    DescriptorRenderer.TEXT);
387    
388            MAP.put(ABSTRACT_MEMBER_NOT_IMPLEMENTED, "{0} must be declared abstract or implement abstract member {1}", RENDER_CLASS_OR_OBJECT,
389                    DescriptorRenderer.TEXT);
390    
391            MAP.put(MANY_IMPL_MEMBER_NOT_IMPLEMENTED, "{0} must override {1} because it inherits many implementations of it",
392                    RENDER_CLASS_OR_OBJECT, DescriptorRenderer.TEXT);
393    
394            MAP.put(CONFLICTING_OVERLOADS, "{1} is already defined in ''{0}''", DescriptorRenderer.TEXT, TO_STRING);
395    
396            MAP.put(FUNCTION_EXPECTED, "Expression ''{0}''{1} cannot be invoked as a function", ELEMENT_TEXT, new Renderer<JetType>() {
397                @NotNull
398                @Override
399                public String render(@NotNull JetType type) {
400                    if (type.isError()) return "";
401                    return " of type '" + type.toString() + "'";
402                }
403            });
404            MAP.put(FUNCTION_CALL_EXPECTED, "Function invocation ''{0}({1})'' expected", ELEMENT_TEXT,new Renderer<Boolean>() {
405                @NotNull
406                @Override
407                public String render(@NotNull Boolean hasValueParameters) {
408                    return hasValueParameters ? "..." : "";
409                }
410            });
411    
412    
413            MAP.put(RESULT_TYPE_MISMATCH, "{0} must return {1} but returns {2}", TO_STRING, RENDER_TYPE, RENDER_TYPE);
414            MAP.put(UNSAFE_INFIX_CALL,
415                    "Infix call corresponds to a dot-qualified call ''{0}.{1}({2})'' which is not allowed on a nullable receiver ''{0}''. " +
416                    "Use '?.'-qualified call instead",
417                    TO_STRING, TO_STRING, TO_STRING);
418    
419            MAP.put(OVERLOAD_RESOLUTION_AMBIGUITY, "Overload resolution ambiguity: {0}", AMBIGUOUS_CALLS);
420            MAP.put(NONE_APPLICABLE, "None of the following functions can be called with the arguments supplied: {0}", AMBIGUOUS_CALLS);
421            MAP.put(CANNOT_COMPLETE_RESOLVE, "Cannot choose among the following candidates without completing type inference: {0}", AMBIGUOUS_CALLS);
422            MAP.put(UNRESOLVED_REFERENCE_WRONG_RECEIVER, "Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: {0}", AMBIGUOUS_CALLS);
423    
424            MAP.put(NO_VALUE_FOR_PARAMETER, "No value passed for parameter {0}", NAME);
425            MAP.put(MISSING_RECEIVER, "A receiver of type {0} is required", RENDER_TYPE);
426            MAP.put(NO_RECEIVER_ADMITTED, "No receiver can be passed to this function or property");
427    
428            MAP.put(CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS, "Cannot create an instance of an abstract class");
429    
430            MAP.put(TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS, "Type inference failed: {0}", TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS_RENDERER);
431            MAP.put(TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER, "Type inference failed: {0}", TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER_RENDERER);
432            MAP.put(TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH, "Type inference failed: {0}", TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH_RENDERER);
433            MAP.put(TYPE_INFERENCE_UPPER_BOUND_VIOLATED, "{0}", TYPE_INFERENCE_UPPER_BOUND_VIOLATED_RENDERER);
434            MAP.put(TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH, "Type inference failed. Expected type mismatch: found: {1} required: {0}", RENDER_TYPE, RENDER_TYPE);
435    
436            MAP.put(WRONG_NUMBER_OF_TYPE_ARGUMENTS, "{0,choice,0#No type arguments|1#Type argument|1<{0,number,integer} type arguments} expected", null);
437            MAP.put(NO_TYPE_ARGUMENTS_ON_RHS, "{0,choice,0#No type arguments|1#Type argument|1<{0,number,integer} type arguments} expected. " +
438                                                               "Use ''{1}'' if you don''t want to pass type arguments", null, TO_STRING);
439    
440            MAP.put(DANGLING_FUNCTION_LITERAL_ARGUMENT_SUSPECTED,
441                    "This expression is treated as an argument to the function call on the previous line. " +
442                    "Separate it with a semicolon (;) if it is not intended to be an argument.");
443    
444            MAP.put(TYPE_PARAMETER_AS_REIFIED, "Cannot use ''{0}'' as reified type parameter. Use a class instead.", NAME);
445    
446            MAP.put(NOT_AN_ANNOTATION_CLASS, "''{0}'' is not an annotation class", TO_STRING);
447            MAP.put(ANNOTATION_CLASS_WITH_BODY, "Body is not allowed for annotation class");
448            MAP.put(INVALID_TYPE_OF_ANNOTATION_MEMBER, "Invalid type of annotation member");
449            MAP.put(NULLABLE_TYPE_OF_ANNOTATION_MEMBER, "An annotation parameter cannot be nullable");
450            MAP.put(ILLEGAL_ANNOTATION_KEYWORD, "''annotation'' keyword is only applicable for class");
451    
452            MAP.put(DEFAULT_VALUE_NOT_ALLOWED_IN_OVERRIDE, "An overriding function is not allowed to specify default values for its parameters");
453    
454    
455            String multipleDefaultsMessage = "More than one overridden descriptor declares a default value for ''{0}''. " +
456                                             "As the compiler can not make sure these values agree, this is not allowed.";
457            MAP.put(MULTIPLE_DEFAULTS_INHERITED_FROM_SUPERTYPES, multipleDefaultsMessage, TO_STRING);
458            MAP.put(MULTIPLE_DEFAULTS_INHERITED_FROM_SUPERTYPES_WHEN_NO_EXPLICIT_OVERRIDE, multipleDefaultsMessage, TO_STRING);
459    
460            MAP.put(PARAMETER_NAME_CHANGED_ON_OVERRIDE, "The corresponding parameter in the supertype ''{0}'' is named ''{1}''. " +
461                                                        "This may cause problems when calling this function with named arguments.", NAME, NAME);
462    
463            MAP.put(DIFFERENT_NAMES_FOR_THE_SAME_PARAMETER_IN_SUPERTYPES,
464                        "Names of the parameter #{1} conflict in the following members of supertypes: ''{0}''" +
465                        "This may cause problems when calling this function with named arguments.", commaSeparated(TO_STRING), TO_STRING);
466    
467            MAP.put(AMBIGUOUS_ANONYMOUS_TYPE_INFERRED, "Right-hand side has anonymous type. Please specify type explicitly", TO_STRING);
468    
469            MAP.put(EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED,
470                    "''{0}'' is a member and an extension at the same time. References to such elements are not allowed", TO_STRING);
471            MAP.put(CALLABLE_REFERENCE_LHS_NOT_A_CLASS, "Callable reference left-hand side cannot be a type parameter");
472    
473            MAP.setImmutable();
474    
475            for (Field field : Errors.class.getFields()) {
476                if (Modifier.isStatic(field.getModifiers())) {
477                    try {
478                        Object fieldValue = field.get(null);
479                        if (fieldValue instanceof DiagnosticFactory) {
480                            if (MAP.get((DiagnosticFactory) fieldValue) == null) {
481                                throw new IllegalStateException("No default diagnostic renderer is provided for " + ((DiagnosticFactory)fieldValue).getName());
482                            }
483                        }
484                    }
485                    catch (IllegalAccessException e) {
486                        throw new IllegalStateException(e);
487                    }
488                }
489            }
490        }
491    
492        private DefaultErrorMessages() {
493        }
494    }