Do not report @deprecated field formal parameters at their declaration.

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

Additionally, fix @deprecated parameters referenced in initializers.

Change-Id: Ibad9c3acaf3991c722bee95ecd58d3e3a04bb4cc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/169903
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Sam Rawlins 2020-10-30 19:10:03 +00:00 committed by commit-bot@chromium.org
parent d70796bae8
commit d0db3b1d23
2 changed files with 62 additions and 2 deletions

View file

@ -102,7 +102,7 @@ class DeprecatedMemberUseVerifier {
void simpleIdentifier(SimpleIdentifier node) {
// Don't report declared identifiers.
if (node.inDeclarationContext()) {
if (_inDeclarationContext(node)) {
return;
}
@ -202,6 +202,22 @@ class DeprecatedMemberUseVerifier {
}
}
/// Returns whether [node] is in a declaration context.
///
/// This accounts for field formal parameters as well, whose identifiers do
/// not consider themselves, for other purposes, to be in a declaration
/// context.
bool _inDeclarationContext(SimpleIdentifier node) {
if (node.inDeclarationContext()) {
return true;
}
var parent = node.parent;
if (parent is FieldFormalParameter && parent.identifier == node) {
return true;
}
return false;
}
void _invocationArguments(Element element, ArgumentList arguments) {
element = element?.declaration;
if (element is ExecutableElement) {
@ -279,7 +295,9 @@ class DeprecatedMemberUseVerifier {
static bool _isLocalParameter(Element element, AstNode node) {
if (element is ParameterElement) {
ExecutableElement definingFunction = element.enclosingElement;
FunctionBody body = node.thisOrAncestorOfType<FunctionBody>();
AstNode body = node.thisOrAncestorOfType<FunctionBody>();
body ??= node.thisOrAncestorOfType<ConstructorInitializer>();
while (body != null) {
ExecutableElement enclosingFunction;
AstNode parent = body.parent;

View file

@ -864,6 +864,48 @@ class A {
]);
}
test_parameter_named_inDefiningConstructor_asFieldFormalParameter() async {
await assertNoErrorsInCode(r'''
class C {
int x;
C({@deprecated this.x});
}
''');
}
test_parameter_named_inDefiningConstructor_assertInitializer() async {
await assertNoErrorsInCode(r'''
class C {
C({@deprecated int y}) : assert(y > 0);
}
''');
}
test_parameter_named_inDefiningConstructor_fieldInitializer() async {
await assertNoErrorsInCode(r'''
class C {
int x;
C({@deprecated int y}) : x = y;
}
''');
}
test_parameter_named_inDefiningConstructor_inFieldFormalParameter_notName() async {
await assertErrorsInCode(r'''
class A {}
@deprecated
class B extends A {}
class C {
A a;
C({B this.a});
}
''', [
error(HintCode.DEPRECATED_MEMBER_USE_FROM_SAME_PACKAGE, 68, 1),
]);
}
test_parameter_named_inDefiningFunction() async {
await assertNoErrorsInCode(r'''
f({@deprecated int x}) => x;