Skip to content

Commit 3502a35

Browse files
bwilkersonCommit Queue
authored andcommitted
Add a context message to not_a_type diagnostics
Fixes #55947 Change-Id: I38b838c59d3c2983ac26efda7a5ab20f3d29485e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/433362 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 5a17591 commit 3502a35

File tree

3 files changed

+103
-9
lines changed

3 files changed

+103
-9
lines changed

pkg/analyzer/lib/src/dart/resolver/named_type_resolver.dart

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:analyzer/src/dart/element/type.dart';
1717
import 'package:analyzer/src/dart/element/type_constraint_gatherer.dart';
1818
import 'package:analyzer/src/dart/element/type_system.dart';
1919
import 'package:analyzer/src/dart/resolver/flow_analysis_visitor.dart';
20+
import 'package:analyzer/src/diagnostic/diagnostic.dart';
2021
import 'package:analyzer/src/diagnostic/diagnostic_factory.dart';
2122
import 'package:analyzer/src/error/codes.dart';
2223
import 'package:analyzer/src/generated/scope_helpers.dart';
@@ -386,11 +387,36 @@ class NamedTypeResolver with ScopeHelpers {
386387
_ErrorHelper(errorReporter).reportNewWithNonType(node);
387388
} else {
388389
node.type = InvalidTypeImpl.instance;
390+
Element? element = importPrefixElement;
391+
String name = node.name.lexeme;
392+
if (importPrefixElement is InstanceElement) {
393+
if (importPrefixElement is InterfaceElement) {
394+
element = importPrefixElement.getNamedConstructor2(name);
395+
}
396+
element ??=
397+
importPrefixElement.getField(name) ??
398+
importPrefixElement.getGetter(name) ??
399+
importPrefixElement.getMethod(name) ??
400+
importPrefixElement.getSetter(name);
401+
}
402+
var fragment = element?.firstFragment;
403+
var source = fragment?.libraryFragment?.source;
404+
var nameOffset = fragment?.nameOffset2;
389405
errorReporter.atOffset(
390406
offset: importPrefix.offset,
391407
length: nameToken.end - importPrefix.offset,
392408
diagnosticCode: CompileTimeErrorCode.NOT_A_TYPE,
393409
arguments: ['${importPrefix.name.lexeme}.${nameToken.lexeme}'],
410+
contextMessages: [
411+
if (source != null && nameOffset != null)
412+
DiagnosticMessageImpl(
413+
filePath: source.fullName,
414+
message: "The declaration of '$name' is here.",
415+
offset: nameOffset,
416+
length: name.length,
417+
url: null,
418+
),
419+
],
394420
);
395421
}
396422
}
@@ -639,11 +665,25 @@ class _ErrorHelper {
639665

640666
if (element != null) {
641667
var errorRange = _getErrorRange(node);
668+
var name = node.name.lexeme;
669+
var fragment = element.firstFragment;
670+
var source = fragment.libraryFragment?.source;
671+
var nameOffset = fragment.nameOffset2;
642672
errorReporter.atOffset(
643673
offset: errorRange.offset,
644674
length: errorRange.length,
645675
diagnosticCode: CompileTimeErrorCode.NOT_A_TYPE,
646-
arguments: [node.name.lexeme],
676+
arguments: [name],
677+
contextMessages: [
678+
if (source != null && nameOffset != null)
679+
DiagnosticMessageImpl(
680+
filePath: source.fullName,
681+
message: "The declaration of '$name' is here.",
682+
offset: nameOffset,
683+
length: name.length,
684+
url: null,
685+
),
686+
],
647687
);
648688
return;
649689
}

pkg/analyzer/test/src/dart/resolution/named_type_test.dart

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,14 @@ import 'dart:math' as prefix;
565565
566566
void f(prefix a) {}
567567
''',
568-
[error(CompileTimeErrorCode.NOT_A_TYPE, 38, 6)],
568+
[
569+
error(
570+
CompileTimeErrorCode.NOT_A_TYPE,
571+
38,
572+
6,
573+
contextMessages: [message(testFile, 22, 6)],
574+
),
575+
],
569576
);
570577

571578
var node = findNode.namedType('prefix a');
@@ -584,7 +591,14 @@ import 'dart:math' as prefix;
584591
585592
void f(prefix<int> a) {}
586593
''',
587-
[error(CompileTimeErrorCode.NOT_A_TYPE, 38, 6)],
594+
[
595+
error(
596+
CompileTimeErrorCode.NOT_A_TYPE,
597+
38,
598+
6,
599+
contextMessages: [message(testFile, 22, 6)],
600+
),
601+
],
588602
);
589603

590604
var node = findNode.namedType('prefix<int>');
@@ -657,7 +671,14 @@ void f(T a) {}
657671
658672
void T() {}
659673
''',
660-
[error(CompileTimeErrorCode.NOT_A_TYPE, 7, 1)],
674+
[
675+
error(
676+
CompileTimeErrorCode.NOT_A_TYPE,
677+
7,
678+
1,
679+
contextMessages: [message(testFile, 21, 1)],
680+
),
681+
],
661682
);
662683

663684
var node = findNode.namedType('T a');
@@ -676,7 +697,14 @@ void f(T<int> a) {}
676697
677698
void T() {}
678699
''',
679-
[error(CompileTimeErrorCode.NOT_A_TYPE, 7, 1)],
700+
[
701+
error(
702+
CompileTimeErrorCode.NOT_A_TYPE,
703+
7,
704+
1,
705+
contextMessages: [message(testFile, 26, 1)],
706+
),
707+
],
680708
);
681709

682710
var node = findNode.namedType('T<int>');

pkg/analyzer/test/src/diagnostics/not_a_type_test.dart

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ class A {
2424
2525
A.foo bar() {}
2626
''',
27-
[error(CompileTimeErrorCode.NOT_A_TYPE, 24, 5)],
27+
[
28+
error(
29+
CompileTimeErrorCode.NOT_A_TYPE,
30+
24,
31+
5,
32+
contextMessages: [message(testFile, 14, 3)],
33+
),
34+
],
2835
);
2936
}
3037

@@ -37,7 +44,14 @@ class A {
3744
3845
A.foo bar() {}
3946
''',
40-
[error(CompileTimeErrorCode.NOT_A_TYPE, 36, 5)],
47+
[
48+
error(
49+
CompileTimeErrorCode.NOT_A_TYPE,
50+
36,
51+
5,
52+
contextMessages: [message(testFile, 24, 3)],
53+
),
54+
],
4155
);
4256
}
4357

@@ -47,7 +61,14 @@ A.foo bar() {}
4761
extension E on int {}
4862
E a;
4963
''',
50-
[error(CompileTimeErrorCode.NOT_A_TYPE, 22, 1)],
64+
[
65+
error(
66+
CompileTimeErrorCode.NOT_A_TYPE,
67+
22,
68+
1,
69+
contextMessages: [message(testFile, 10, 1)],
70+
),
71+
],
5172
);
5273

5374
var node = findNode.namedType('E a;');
@@ -67,7 +88,12 @@ main() {
6788
f v = null;
6889
}''',
6990
[
70-
error(CompileTimeErrorCode.NOT_A_TYPE, 18, 1),
91+
error(
92+
CompileTimeErrorCode.NOT_A_TYPE,
93+
18,
94+
1,
95+
contextMessages: [message(testFile, 0, 1)],
96+
),
7197
error(WarningCode.UNUSED_LOCAL_VARIABLE, 20, 1),
7298
],
7399
);

0 commit comments

Comments
 (0)