Issue 38565. Revert incomplete annotation constructor invocation inference implementation.

This partially reverts https://dart-review.googlesource.com/c/sdk/+/118901

R=brianwilkerson@google.com

Bug: https://github.com/dart-lang/sdk/issues/38565
Change-Id: I4d259a380985ce6e0670fc644594054b820b9bf5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/119534
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2019-10-01 20:44:26 +00:00 committed by commit-bot@chromium.org
parent 431176ea26
commit cb9a35ca53
3 changed files with 25 additions and 55 deletions

View file

@ -19,9 +19,7 @@ import 'package:analyzer/src/dart/ast/ast.dart'
import 'package:analyzer/src/dart/ast/token.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
import 'package:analyzer/src/dart/element/member.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_algebra.dart';
import 'package:analyzer/src/dart/resolver/extension_member_resolver.dart';
import 'package:analyzer/src/dart/resolver/method_invocation_resolver.dart';
import 'package:analyzer/src/dart/resolver/resolution_result.dart';
@ -1254,44 +1252,6 @@ class ElementResolver extends SimpleAstVisitor<void> {
_resolver.errorReporter.reportErrorForToken(errorCode, token, arguments);
}
/// Resolve the [constructorName] to the constructor in the class [element].
/// Perform inference using [argumentList].
ConstructorElement _resolveAnnotationConstructor(
ClassElement element,
String constructorName,
ArgumentList argumentList,
) {
var constructor = constructorName != null
? element.getNamedConstructor(constructorName)
: element.unnamedConstructor;
if (constructor == null) {
return null;
}
if (!constructor.isAccessibleIn(_definingLibrary)) {
return null;
}
var typeParameters = element.typeParameters;
if (typeParameters.isEmpty) {
return constructor;
}
var typeArgs = _resolver.typeSystem.inferGenericFunctionOrType(
typeParameters: typeParameters,
parameters: constructor.parameters,
declaredReturnType: null,
argumentTypes: argumentList.arguments.map((a) => a.staticType).toList(),
contextReturnType: null,
isConst: true,
errorReporter: _resolver.errorReporter,
errorNode: argumentList,
);
return ExecutableMember.from2(
constructor,
Substitution.fromPairs(typeParameters, typeArgs),
);
}
void _resolveAnnotationConstructorInvocationArguments(
Annotation annotation, ConstructorElement constructor) {
ArgumentList argumentList = annotation.arguments;
@ -1338,11 +1298,7 @@ class ElementResolver extends SimpleAstVisitor<void> {
}
// Class(args)
if (element1 is ClassElement) {
constructor = _resolveAnnotationConstructor(
element1,
null,
annotation.arguments,
);
constructor = element1.type.lookUpConstructor(null, _definingLibrary);
} else if (element1 == null) {
undefined = true;
}
@ -1370,11 +1326,8 @@ class ElementResolver extends SimpleAstVisitor<void> {
}
// Class.constructor(args)
if (element1 is ClassElement) {
constructor = _resolveAnnotationConstructor(
element1,
nameNode2.name,
annotation.arguments,
);
constructor =
element1.type.lookUpConstructor(nameNode2.name, _definingLibrary);
nameNode2.staticElement = constructor;
}
if (element1 == null && element2 == null) {
@ -1399,11 +1352,7 @@ class ElementResolver extends SimpleAstVisitor<void> {
return;
}
// prefix.Class.constructor(args)
constructor = _resolveAnnotationConstructor(
element2,
name3,
annotation.arguments,
);
constructor = element2.type.lookUpConstructor(name3, _definingLibrary);
nameNode3.staticElement = constructor;
} else if (element2 == null) {
undefined = true;

View file

@ -16,6 +16,26 @@ main() {
/// not obvious where to put the test otherwise.
@reflectiveTest
class IssuesTest extends DriverResolutionTest {
/// https://github.com/dart-lang/sdk/issues/38565
///
/// The issue was that type inference for annotation instantiation
/// was done incorrectly, without resolving arguments to the annotation
/// constructor. So, we were trying to perform type inference using null
/// types of arguments.
test_issue38565() async {
await resolveTestCode('''
class A<T> {
const A(int a);
}
class C {
@A(0)
int field;
}
''');
// Should not crash.
}
/// https://github.com/dart-lang/sdk/issues/38589
test_issue38589() async {
await resolveTestCode('''

View file

@ -103,6 +103,7 @@ C c;
expect(value.getField('a').getField('f').toIntValue(), 42);
}
@FailingTest(reason: 'Reverted because of dartbug.com/38565')
test_sameLibrary_genericClass_constructor_unnamed() async {
await assertNoErrorsInCode(r'''
class A<T> {