From 4e29f0b339afde5393f53b5677f417b69330cbd0 Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Fri, 24 Mar 2023 17:36:28 +0000 Subject: [PATCH] Add documentation for two more diagnostics Also adds a dropped link target so that we don't break existing users. Change-Id: I98e5e9fe2e51730150acfab889245a7b9f6b11d3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290180 Commit-Queue: Brian Wilkerson Reviewed-by: Konstantin Shcheglov --- pkg/analyzer/lib/src/error/codes.g.dart | 3 + pkg/analyzer/messages.yaml | 122 +++++++++++++++++ pkg/analyzer/tool/diagnostics/diagnostics.md | 131 +++++++++++++++++++ 3 files changed, 256 insertions(+) diff --git a/pkg/analyzer/lib/src/error/codes.g.dart b/pkg/analyzer/lib/src/error/codes.g.dart index 0369aa19271..3f62ac27201 100644 --- a/pkg/analyzer/lib/src/error/codes.g.dart +++ b/pkg/analyzer/lib/src/error/codes.g.dart @@ -3010,6 +3010,9 @@ class CompileTimeErrorCode extends AnalyzerErrorCode { 'MIXIN_CLASS_DECLARATION_EXTENDS_NOT_OBJECT', "The class '{0}' can't be declared a mixin because it extends a class " "other than 'Object'.", + correctionMessage: + "Try removing the 'mixin' modifier or changing the superclass to " + "'Object'.", ); /// Parameters: diff --git a/pkg/analyzer/messages.yaml b/pkg/analyzer/messages.yaml index fd689124713..83aa37e5216 100644 --- a/pkg/analyzer/messages.yaml +++ b/pkg/analyzer/messages.yaml @@ -2834,6 +2834,7 @@ CompileTimeErrorCode: CONST_SET_ELEMENT_NOT_PRIMITIVE_EQUALITY: problemMessage: "An element in a constant set can't override the '==' operator, or 'hashCode', but the type '{0}' does." correctionMessage: "Try using a different value for the element, or removing the keyword 'const' from the set." + previousName: CONST_SET_ELEMENT_TYPE_IMPLEMENTS_EQUALS hasPublishedDocs: true comment: |- Parameters: @@ -9598,9 +9599,62 @@ CompileTimeErrorCode: ``` MIXIN_CLASS_DECLARATION_EXTENDS_NOT_OBJECT: problemMessage: "The class '{0}' can't be declared a mixin because it extends a class other than 'Object'." + correctionMessage: Try removing the 'mixin' modifier or changing the superclass to 'Object'. comment: |- Parameters: 0: the name of the mixin class that is invalid + documentation: |- + #### Description + + The analyzer produces this diagnostic when a class that is marked with + the `mixin` modifier extends a class other than `Object`. A mixin class + can't have a superclass other than `Object`. + + #### Example + + The following code produces this diagnostic because the class `B`, which + has the modifier `mixin`, extends `A`: + + ```dart + class A {} + + mixin class B extends [!A!] {} + ``` + + #### Common fixes + + If you want the class to be used as a mixin, then change the superclass to + `Object`, either explicitly or by removing the extends clause: + + ```dart + class A {} + + mixin class B {} + ``` + + If the class needs to have a superclass other than `Object`, then remove + the `mixin` modifier: + + ```dart + class A {} + + class B extends A {} + ``` + + If you need both a mixin and a subclass of a class other than `Object`, + then move the members of the subclass to a new mixin, remove the `mixin` + modifier from the subclass, and apply the new mixin to the subclass: + + ```dart + class A {} + + class B extends A with M {} + + mixin M {} + ``` + + Depending on the members of the subclass this might require adding an `on` + clause to the mixin. MIXIN_CLASS_DECLARES_CONSTRUCTOR: problemMessage: "The class '{0}' can't be used as a mixin because it declares a constructor." hasPublishedDocs: true @@ -13693,6 +13747,74 @@ CompileTimeErrorCode: 0: the operand type 1: the parameter type of the invoked operator 2: the name of the invoked operator + documentation: |- + #### Description + + The analyzer produces this diagnostic when the operand of a relational + pattern has a type that isn't assignable to the parameter of the operator + that will be invoked. + + #### Example + + The following code produces this diagnostic because the operand in the + relational pattern (`0`) is an `int`, but the `>` operator defined in `C` + expects an object of type `C`: + + ```dart + class C { + const C(); + + bool operator >(C other) => true; + } + + void f(C c) { + switch (c) { + case > [!0!]: + print('positive'); + } + } + ``` + + #### Common fixes + + If the switch is using the correct value, then change the case to compare + the value to the right type of object: + + ```dart + class C { + const C(); + + bool operator >(C other) => true; + } + + void f(C c) { + switch (c) { + case > const C(): + print('positive'); + } + } + ``` + + If the switch is using the wrong value, then change the expression used to + compute the value being matched: + + ```dart + class C { + const C(); + + bool operator >(C other) => true; + + int get toInt => 0; + } + + void f(C c) { + switch (c.toInt) { + case > 0: + print('positive'); + } + } + ``` + RELATIONAL_PATTERN_OPERATOR_RETURN_TYPE_NOT_ASSIGNABLE_TO_BOOL: problemMessage: The return type of operators used in relational patterns must be assignable to 'bool'. correctionMessage: Try updating the operator declaration to return 'bool'. diff --git a/pkg/analyzer/tool/diagnostics/diagnostics.md b/pkg/analyzer/tool/diagnostics/diagnostics.md index 41b2dc24f02..1fed3edfd50 100644 --- a/pkg/analyzer/tool/diagnostics/diagnostics.md +++ b/pkg/analyzer/tool/diagnostics/diagnostics.md @@ -3257,6 +3257,8 @@ const c = 'c'; ### const_set_element_not_primitive_equality +_(Previously known as `const_set_element_type_implements_equals`)_ + _An element in a constant set can't override the '==' operator, or 'hashCode', but the type '{0}' does._ @@ -12668,6 +12670,63 @@ mixin M on A { abstract class B extends A with M {} {% endprettify %} +### mixin_class_declaration_extends_not_object + +_The class '{0}' can't be declared a mixin because it extends a class other than +'Object'._ + +#### Description + +The analyzer produces this diagnostic when a class that is marked with +the `mixin` modifier extends a class other than `Object`. A mixin class +can't have a superclass other than `Object`. + +#### Example + +The following code produces this diagnostic because the class `B`, which +has the modifier `mixin`, extends `A`: + +{% prettify dart tag=pre+code %} +class A {} + +mixin class B extends [!A!] {} +{% endprettify %} + +#### Common fixes + +If you want the class to be used as a mixin, then change the superclass to +`Object`, either explicitly or by removing the extends clause: + +{% prettify dart tag=pre+code %} +class A {} + +mixin class B {} +{% endprettify %} + +If the class needs to have a superclass other than `Object`, then remove +the `mixin` modifier: + +{% prettify dart tag=pre+code %} +class A {} + +class B extends A {} +{% endprettify %} + +If you need both a mixin and a subclass of a class other than `Object`, +then move the members of the subclass to a new mixin, remove the `mixin` +modifier from the subclass, and apply the new mixin to the subclass: + +{% prettify dart tag=pre+code %} +class A {} + +class B extends A with M {} + +mixin M {} +{% endprettify %} + +Depending on the members of the subclass this might require adding an `on` +clause to the mixin. + ### mixin_class_declares_constructor _The class '{0}' can't be used as a mixin because it declares a constructor._ @@ -17247,6 +17306,78 @@ void f(int? x) { Rewrite the code to not use a refutable pattern in an irrefutable context. +### relational_pattern_operand_type_not_assignable + +_The constant expression type '{0}' is not assignable to the parameter type +'{1}' of the '{2}' operator._ + +#### Description + +The analyzer produces this diagnostic when the operand of a relational +pattern has a type that isn't assignable to the parameter of the operator +that will be invoked. + +#### Example + +The following code produces this diagnostic because the operand in the +relational pattern (`0`) is an `int`, but the `>` operator defined in `C` +expects an object of type `C`: + +{% prettify dart tag=pre+code %} +class C { + const C(); + + bool operator >(C other) => true; +} + +void f(C c) { + switch (c) { + case > [!0!]: + print('positive'); + } +} +{% endprettify %} + +#### Common fixes + +If the switch is using the correct value, then change the case to compare +the value to the right type of object: + +{% prettify dart tag=pre+code %} +class C { + const C(); + + bool operator >(C other) => true; +} + +void f(C c) { + switch (c) { + case > const C(): + print('positive'); + } +} +{% endprettify %} + +If the switch is using the wrong value, then change the expression used to +compute the value being matched: + +{% prettify dart tag=pre+code %} +class C { + const C(); + + bool operator >(C other) => true; + + int get toInt => 0; +} + +void f(C c) { + switch (c.toInt) { + case > 0: + print('positive'); + } +} +{% endprettify %} + ### relational_pattern_operator_return_type_not_assignable_to_bool _The return type of operators used in relational patterns must be assignable to