Skip to content

Commit 3a17beb

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Issue 39028. Guard against mixin constructor in FieldFormalParameter completion.
[email protected] Bug: #39028 Change-Id: I78e4df6baa8424231aeb2c8044d9cb5c685dcdbe Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/125524 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 2a5d00b commit 3a17beb

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

pkg/analysis_server/lib/src/services/completion/dart/field_formal_contributor.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,14 @@ class FieldFormalContributor extends DartCompletionContributor {
2525
return const <CompletionSuggestion>[];
2626
}
2727

28-
// If this is a constructor declaration
29-
// then compute fields already referenced
30-
ConstructorDeclaration constructorDecl =
31-
node.thisOrAncestorOfType<ConstructorDeclaration>();
32-
if (constructorDecl == null) {
28+
ConstructorDeclaration constructor = node.thisOrAncestorOfType();
29+
if (constructor == null) {
3330
return const <CompletionSuggestion>[];
3431
}
3532

3633
// Compute the list of fields already referenced in the constructor
3734
List<String> referencedFields = new List<String>();
38-
for (FormalParameter param in constructorDecl.parameters.parameters) {
35+
for (FormalParameter param in constructor.parameters.parameters) {
3936
if (param is DefaultFormalParameter &&
4037
param.parameter is FieldFormalParameter) {
4138
param = (param as DefaultFormalParameter).parameter;
@@ -51,11 +48,14 @@ class FieldFormalContributor extends DartCompletionContributor {
5148
}
5249
}
5350

51+
ClassDeclaration class_ = constructor.thisOrAncestorOfType();
52+
if (class_ == null) {
53+
return const <CompletionSuggestion>[];
54+
}
55+
5456
// Add suggestions for fields that are not already referenced
55-
ClassDeclaration classDecl =
56-
constructorDecl.thisOrAncestorOfType<ClassDeclaration>();
5757
List<CompletionSuggestion> suggestions = <CompletionSuggestion>[];
58-
for (ClassMember member in classDecl.members) {
58+
for (ClassMember member in class_.members) {
5959
if (member is FieldDeclaration && !member.isStatic) {
6060
for (VariableDeclaration varDecl in member.fields.variables) {
6161
SimpleIdentifier fieldId = varDecl.name;

pkg/analysis_server/test/services/completion/dart/field_formal_contributor_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ class FieldFormalContributorTest extends DartCompletionContributorTest {
2222
return new FieldFormalContributor();
2323
}
2424

25+
/// https://github.com/dart-lang/sdk/issues/39028
26+
test_mixin_constructor() async {
27+
addTestSource('''
28+
mixin M {
29+
M(this.^);
30+
}
31+
''');
32+
await computeSuggestions();
33+
expect(suggestions, isEmpty);
34+
}
35+
2536
test_ThisExpression_constructor_param() async {
2637
// SimpleIdentifier FieldFormalParameter FormalParameterList
2738
addTestSource('''

0 commit comments

Comments
 (0)