Skip to content

Analyzer should reject prefix shadowed by parameter #42620

@stereotype441

Description

@stereotype441

Consider the following code:

import 'dart:async' as a;

g(int x) async => x;
f(int a) {
  a.Future<int> x = g(a);
  return x;
}
main() async {
  print(await f(0));
}

The front end rejects this code with the following error:

../../tmp/test.dart:5:3: Error: 'a.Future' can't be used as a type because 'a' doesn't refer to an import prefix.
  a.Future<int> x = g(a);
  ^^^^^^^^

The analyzer accepts it, offering only this hint:

  hint • Unused import: 'dart:async'. • /usr/local/google/home/paulberry/tmp/test.dart:1:8 • unused_import

It seems that for type analysis, the analyzer is considering the a in a.Future to refer to the import of dart:async, but for determining whether the import is used, it's considering it to refer to the a parameter of f.

The spec seems pretty clearly in agreement with the front end. In the section "Static Types", it says:

A type T is malformed iff:

  • T has the form id or the form prefix.id, and in the enclosing lexical scope, the name id (respectively prefix.id) does not denote a type.

Note that although the example above is contrived, this bug did arise in a real-world circumstance for me; I was attempting to write a method taking an argument called path, in a file that contained import 'package:path/path.dart' as path, and inside that method I attempted to declare a variable of type path.Context. The analyzer did not flag an error (nor did I realize my own mistake); I didn't see an error until I tried to run the program.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions