dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.07k stars 1.56k forks source link

Error message on variable shadowing confusing #34200

Open alan-knight opened 6 years ago

alan-knight commented 6 years ago

Version 2_0_0_dev_69_5 , Linux

Code like

handle(response) {
  print(response);
  ... lots of stuff ...
  ... another request ...
  var response = ...
}

Gives an error message on the second line that the variable "response" cannot be used before it is declared. This is confusing. It makes sense the compiler because response has been shadowed, and the shadowed variable isn't visible yet, but could be quite confusing for users, as there is clearly a variable response and it's clearly in scope from the line directly before.

vsmenon commented 5 years ago

@alan-knight - do you remember if this is a compile time error?

lrhn commented 3 years ago

It's definitely a compile-time error. (I think it always was).

For comparison, this is what dart2js writes as error:

Error: Can't declare 'response' because it was already used in this scope.
  var response = 42;
      ^^^^^^^^
lib/main.dart:2:9:
Info: Previous use of 'response'.
  print(response);
        ^^^^^^^^