Analyzer: Remove error on class-used-as-mixin referencing super

Fixes https://github.com/dart-lang/sdk/issues/34806

Change-Id: I1cfd0b9c0699734b172aa62f7a04ab533a6cd03f
Fixed: 34806
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151099
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Sam Rawlins 2020-06-13 15:11:25 +00:00 committed by commit-bot@chromium.org
parent 3dc27a55fa
commit 32a93e2147
9 changed files with 3 additions and 120 deletions

View file

@ -51,37 +51,6 @@ main() {
});
}
Future<void> test_super_mixins_disabled() async {
var pathname = sourcePath('test.dart');
writeFile(pathname, '''
class Test extends Object with C {
void foo() {}
}
abstract class B {
void foo() {}
}
abstract class C extends B {
void bar() {
super.foo();
}
}
''');
standardAnalysisSetup();
await analysisFinished;
expect(currentAnalysisErrors[pathname], isList);
var errors = currentAnalysisErrors[pathname];
expect(errors, hasLength(2));
var allErrorMessages = errors.map((AnalysisError e) => e.message).toSet();
expect(
allErrorMessages,
contains(
"The class 'C' can't be used as a mixin because it extends a class other than Object."));
expect(
allErrorMessages,
contains(
"The class 'C' can't be used as a mixin because it references 'super'."));
}
@failingTest
Future<void> test_super_mixins_enabled() async {
// We see errors here with the new driver (#28870).

View file

@ -235,7 +235,6 @@ const List<ErrorCode> errorCodeValues = [
CompileTimeErrorCode.MIXIN_INSTANTIATE,
CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS,
CompileTimeErrorCode.MIXIN_OF_NON_CLASS,
CompileTimeErrorCode.MIXIN_REFERENCES_SUPER,
CompileTimeErrorCode.MIXIN_SUPER_CLASS_CONSTRAINT_DEFERRED_CLASS,
CompileTimeErrorCode.MIXIN_SUPER_CLASS_CONSTRAINT_DISALLOWED_CLASS,
CompileTimeErrorCode.MIXIN_SUPER_CLASS_CONSTRAINT_NON_INTERFACE,

View file

@ -4135,16 +4135,6 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
'MIXIN_OF_NON_CLASS', "Classes can only mix in mixins and classes.",
hasPublishedDocs: true);
/**
* 9 Mixins: It is a compile-time error if a declared or derived mixin refers
* to super.
*/
static const CompileTimeErrorCode MIXIN_REFERENCES_SUPER =
CompileTimeErrorCode(
'MIXIN_REFERENCES_SUPER',
"The class '{0}' can't be used as a mixin because it references "
"'super'.");
static const CompileTimeErrorCode
MIXIN_SUPER_CLASS_CONSTRAINT_DEFERRED_CLASS = CompileTimeErrorCode(
'MIXIN_SUPER_CLASS_CONSTRAINT_DEFERRED_CLASS',

View file

@ -1301,8 +1301,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
* Verify that all classes of the given [withClause] are valid.
*
* See [CompileTimeErrorCode.MIXIN_CLASS_DECLARES_CONSTRUCTOR],
* [CompileTimeErrorCode.MIXIN_INHERITS_FROM_NOT_OBJECT], and
* [CompileTimeErrorCode.MIXIN_REFERENCES_SUPER].
* [CompileTimeErrorCode.MIXIN_INHERITS_FROM_NOT_OBJECT].
*/
bool _checkForAllMixinErrorCodes(WithClause withClause) {
if (withClause == null) {
@ -1342,9 +1341,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
if (_checkForMixinInheritsNotFromObject(mixinName, mixinElement)) {
problemReported = true;
}
if (_checkForMixinReferencesSuper(mixinName, mixinElement)) {
problemReported = true;
}
}
}
}
@ -3297,24 +3293,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
return false;
}
/**
* Verify that the given mixin does not reference 'super'. The [mixinName] is
* the node to report problem on. The [mixinElement] is the mixing to
* evaluate.
*
* See [CompileTimeErrorCode.MIXIN_REFERENCES_SUPER].
*/
bool _checkForMixinReferencesSuper(
TypeName mixinName, ClassElement mixinElement) {
if (mixinElement.hasReferenceToSuper) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.MIXIN_REFERENCES_SUPER,
mixinName,
[mixinElement.name]);
}
return false;
}
/// Check that superclass constrains for the mixin type of [mixinName] at
/// the [mixinIndex] position in the mixins list are satisfied by the
/// [_enclosingClass], or a previous mixin.

View file

@ -861,15 +861,13 @@ class C = Object with A;''');
}
test_isValidMixin_super() async {
await assertErrorsInCode(r'''
await assertNoErrorsInCode(r'''
class A {
toString() {
return super.toString();
}
}
class C = Object with A;''', [
error(CompileTimeErrorCode.MIXIN_REFERENCES_SUPER, 82, 1),
]);
class C = Object with A;''');
verifyTestResolved();
var a = findElement.class_('A');

View file

@ -1,37 +0,0 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/driver_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(MixinReferencesSuperTest);
});
}
@reflectiveTest
class MixinReferencesSuperTest extends DriverResolutionTest {
test_mixin_references_super() async {
await assertErrorsInCode(r'''
class A {
toString() => super.toString();
}
class B extends Object with A {}
''', [
error(CompileTimeErrorCode.MIXIN_REFERENCES_SUPER, 74, 1),
]);
}
test_mixin_unconstrained_references_super() async {
await assertNoErrorsInCode(r'''
mixin A {
toString() => super.toString();
}
class B extends Object with A {}
''');
}
}

View file

@ -293,7 +293,6 @@ import 'mixin_inherits_from_not_object_test.dart'
import 'mixin_of_disallowed_class_test.dart' as mixin_of_disallowed_class;
import 'mixin_of_non_class_test.dart' as mixin_of_non_class;
import 'mixin_on_sealed_class_test.dart' as mixin_on_sealed_class;
import 'mixin_references_super_test.dart' as mixin_references_super;
import 'mixin_super_class_constraint_non_interface_test.dart'
as mixin_super_class_constraint_non_interface;
import 'mixin_with_non_class_superclass_test.dart'
@ -721,7 +720,6 @@ main() {
mixin_of_disallowed_class.main();
mixin_of_non_class.main();
mixin_on_sealed_class.main();
mixin_references_super.main();
mixin_super_class_constraint_non_interface.main();
mixin_with_non_class_superclass.main();
must_be_a_native_function_type.main();

View file

@ -76,14 +76,8 @@ class P2 {
class C = Object with M;
class D = Object with P0;
// ^^
// [analyzer] COMPILE_TIME_ERROR.MIXIN_REFERENCES_SUPER
class E = Object with M, P1;
// ^^
// [analyzer] COMPILE_TIME_ERROR.MIXIN_REFERENCES_SUPER
class F = Object with P2, M;
// ^^
// [analyzer] COMPILE_TIME_ERROR.MIXIN_REFERENCES_SUPER
main() {
var p1 = new P1();

View file

@ -76,14 +76,8 @@ class P2 {
class C = Object with M;
class D = Object with P0;
// ^^
// [analyzer] COMPILE_TIME_ERROR.MIXIN_REFERENCES_SUPER
class E = Object with M, P1;
// ^^
// [analyzer] COMPILE_TIME_ERROR.MIXIN_REFERENCES_SUPER
class F = Object with P2, M;
// ^^
// [analyzer] COMPILE_TIME_ERROR.MIXIN_REFERENCES_SUPER
main() {
var p1 = new P1();