Skip to content

Commit 27ab647

Browse files
committed
Report errors for the new top-level inference rules.
We also stop type inference most of these cases with task based analysis. Summary based analysis is not changed yet. [email protected] BUG= Review-Url: https://codereview.chromium.org/2782533002 .
1 parent 7c8a0a7 commit 27ab647

16 files changed

+1213
-1364
lines changed

pkg/analyzer/lib/error/error.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,13 @@ const List<ErrorCode> errorCodeValues = const [
638638
StrongModeCode.NO_DEFAULT_BOUNDS,
639639
StrongModeCode.NON_GROUND_TYPE_CHECK_INFO,
640640
StrongModeCode.NOT_INSTANTIATED_BOUND,
641+
StrongModeCode.TOP_LEVEL_CYCLE,
642+
StrongModeCode.TOP_LEVEL_FUNCTION_LITERAL_BLOCK,
643+
StrongModeCode.TOP_LEVEL_FUNCTION_LITERAL_PARAMETER,
644+
StrongModeCode.TOP_LEVEL_IDENTIFIER_NO_TYPE,
645+
StrongModeCode.TOP_LEVEL_INSTANCE_GETTER,
646+
StrongModeCode.TOP_LEVEL_TYPE_ARGUMENTS,
647+
StrongModeCode.TOP_LEVEL_UNSUPPORTED,
641648
StrongModeCode.UNSAFE_BLOCK_CLOSURE_INFERENCE,
642649
TodoCode.TODO,
643650
];

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class AnalysisDriver implements AnalysisDriverGeneric {
7272
/**
7373
* The version of data format, should be incremented on every format change.
7474
*/
75-
static const int DATA_VERSION = 27;
75+
static const int DATA_VERSION = 28;
7676

7777
/**
7878
* The number of exception contexts allowed to write. Once this field is

pkg/analyzer/lib/src/error/codes.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4951,6 +4951,50 @@ class StrongModeCode extends ErrorCode {
49514951
"Type parameter bound types must be instantiated.",
49524952
"Try adding type arguments.");
49534953

4954+
static const StrongModeCode TOP_LEVEL_CYCLE = const StrongModeCode(
4955+
ErrorType.COMPILE_TIME_ERROR,
4956+
'TOP_LEVEL_CYCLE',
4957+
"The type of '{0}' can't be inferred because it depends on itself through the cycle: {1}.",
4958+
"Try adding an explicit type to one or more of the variables in the cycle in order to break the cycle.");
4959+
4960+
static const StrongModeCode TOP_LEVEL_FUNCTION_LITERAL_BLOCK =
4961+
const StrongModeCode(
4962+
ErrorType.COMPILE_TIME_ERROR,
4963+
'TOP_LEVEL_FUNCTION_LITERAL_BLOCK',
4964+
"The type of the function literal can't be inferred because the literal has a block as its body.",
4965+
"Try adding an explicit type to the variable.");
4966+
4967+
static const StrongModeCode TOP_LEVEL_FUNCTION_LITERAL_PARAMETER =
4968+
const StrongModeCode(
4969+
ErrorType.COMPILE_TIME_ERROR,
4970+
'TOP_LEVEL_FUNCTION_LITERAL_PARAMETER',
4971+
"The type of '{0}' can't be inferred because the parameter '{1}' does not have an explicit type.",
4972+
"Try adding an explicit type to the parameter '{1}', or add an explicit type for '{0}'.");
4973+
4974+
static const StrongModeCode TOP_LEVEL_IDENTIFIER_NO_TYPE = const StrongModeCode(
4975+
ErrorType.COMPILE_TIME_ERROR,
4976+
'TOP_LEVEL_IDENTIFIER_NO_TYPE',
4977+
"The type of '{0}' can't be inferred because the type of '{1}' couldn't be inferred.",
4978+
"Try adding an explicit type to either the variable '{0}' or the variable '{1}'.");
4979+
4980+
static const StrongModeCode TOP_LEVEL_INSTANCE_GETTER = const StrongModeCode(
4981+
ErrorType.COMPILE_TIME_ERROR,
4982+
'TOP_LEVEL_INSTANCE_GETTER',
4983+
"The type of '{0}' can't be inferred because of the use of the instance getter '{1}'.",
4984+
"Try removing the use of the instance getter {1}, or add an explicit type for '{0}'.");
4985+
4986+
static const StrongModeCode TOP_LEVEL_TYPE_ARGUMENTS = const StrongModeCode(
4987+
ErrorType.COMPILE_TIME_ERROR,
4988+
'TOP_LEVEL_TYPE_ARGUMENTS',
4989+
"The type of '{0}' can't be inferred because type arguments were not given for '{1}'.",
4990+
"Try adding type arguments for '{1}', or add an explicit type for '{0}'.");
4991+
4992+
static const StrongModeCode TOP_LEVEL_UNSUPPORTED = const StrongModeCode(
4993+
ErrorType.COMPILE_TIME_ERROR,
4994+
'TOP_LEVEL_UNSUPPORTED',
4995+
"The type of '{0}' can't be inferred because {1} expressions aren't supported.",
4996+
"Try adding an explicit type for '{0}'.");
4997+
49544998
static const StrongModeCode UNSAFE_BLOCK_CLOSURE_INFERENCE = const StrongModeCode(
49554999
ErrorType.STATIC_WARNING,
49565000
'UNSAFE_BLOCK_CLOSURE_INFERENCE',

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5021,6 +5021,12 @@ class ResolverVisitor extends ScopedVisitor {
50215021
*/
50225022
bool resolveOnlyCommentInFunctionBody = false;
50235023

5024+
/**
5025+
* This flag is set to `true` while the type of a top-level variable or a
5026+
* class field is being inferred using its initializer.
5027+
*/
5028+
bool isTopLevelInference;
5029+
50245030
/**
50255031
* Body of the function currently being analyzed, if any.
50265032
*/
@@ -5050,7 +5056,7 @@ class ResolverVisitor extends ScopedVisitor {
50505056
*/
50515057
ResolverVisitor(LibraryElement definingLibrary, Source source,
50525058
TypeProvider typeProvider, AnalysisErrorListener errorListener,
5053-
{Scope nameScope})
5059+
{Scope nameScope, this.isTopLevelInference: false})
50545060
: super(definingLibrary, source, typeProvider, errorListener,
50555061
nameScope: nameScope) {
50565062
AnalysisOptions options = definingLibrary.context.analysisOptions;
@@ -5550,11 +5556,14 @@ class ResolverVisitor extends ScopedVisitor {
55505556

55515557
@override
55525558
Object visitBlockFunctionBody(BlockFunctionBody node) {
5559+
bool wasTopLevelInference = isTopLevelInference;
5560+
isTopLevelInference = false;
55535561
_overrideManager.enterScope();
55545562
try {
55555563
inferenceContext.pushReturnContext(node);
55565564
super.visitBlockFunctionBody(node);
55575565
} finally {
5566+
isTopLevelInference = wasTopLevelInference;
55585567
_overrideManager.exitScope();
55595568
inferenceContext.popReturnContext(node);
55605569
}
@@ -5873,10 +5882,13 @@ class ResolverVisitor extends ScopedVisitor {
58735882

58745883
@override
58755884
Object visitFieldDeclaration(FieldDeclaration node) {
5885+
bool wasTopLevelInference = isTopLevelInference;
5886+
isTopLevelInference = node.fields.type == null;
58765887
_overrideManager.enterScope();
58775888
try {
58785889
super.visitFieldDeclaration(node);
58795890
} finally {
5891+
isTopLevelInference = wasTopLevelInference;
58805892
Map<VariableElement, DartType> overrides =
58815893
_overrideManager.captureOverrides(node.fields);
58825894
_overrideManager.exitScope();
@@ -6379,10 +6391,13 @@ class ResolverVisitor extends ScopedVisitor {
63796391

63806392
@override
63816393
Object visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
6394+
bool wasTopLevelInference = isTopLevelInference;
6395+
isTopLevelInference = node.variables.type == null;
63826396
_overrideManager.enterScope();
63836397
try {
63846398
super.visitTopLevelVariableDeclaration(node);
63856399
} finally {
6400+
isTopLevelInference = wasTopLevelInference;
63866401
Map<VariableElement, DartType> overrides =
63876402
_overrideManager.captureOverrides(node.variables);
63886403
_overrideManager.exitScope();
@@ -6661,6 +6676,11 @@ class ResolverVisitor extends ScopedVisitor {
66616676
uninstantiatedType is FunctionType &&
66626677
uninstantiatedType.typeFormals.isNotEmpty &&
66636678
ts is StrongTypeSystemImpl) {
6679+
if (isTopLevelInference) {
6680+
if (uninstantiatedType.typeFormals.isNotEmpty) {
6681+
return null;
6682+
}
6683+
}
66646684
return ts.inferGenericFunctionOrType/*<FunctionType>*/(
66656685
uninstantiatedType,
66666686
ParameterElement.EMPTY_LIST,

0 commit comments

Comments
 (0)