Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local shadow causes 'cannot be referenced before it is declared' #3801

Open
mattrberry opened this issue May 14, 2024 · 3 comments
Open

Local shadow causes 'cannot be referenced before it is declared' #3801

mattrberry opened this issue May 14, 2024 · 3 comments
Labels
request Requests to resolve a particular developer problem

Comments

@mattrberry
Copy link

class C {
  final int f;
  C(this.f);
  void m() {
    print(f);
    final f = this.f;
  }
}
main.dart:10:11: Error: Local variable 'f' can't be referenced before it is declared.
    print(f);
          ^
main.dart:11:11: Context: This is the declaration of the variable 'f'.
    final f = this.f;
          ^

In this example, f is both a local and instance variable. I see this pattern used a lot to perform type checks on the instance variable. While I understand the error message and its value, I feel like it doesn't make much sense when the local just shadows the instance variable of the same name.

The following code produces roughly the same error. I feel like it should also work, but it's a little weaker of an argument

void f(int p) {
  print(p);
  final p = 1;
}
@bwilkerson
Copy link
Member

@dart-lang/language-team

@lrhn
Copy link
Member

lrhn commented May 14, 2024

Working as currently intended. The scope of final f is the entire block it's part of.

Not all local variables have that property, those introduced by a collection element (for (var x in ...) or if (e case var x) ...) are only in scope in their "body element", but stand-alone variable declarations do.

So this is a request to change the current behavior, which means that a variable declaration's variable will only be in scope after the declaration.
It's not a big change, since it's already not allowed to refer to the variable before that, so it would just mean that any place we currently say that "referencing x is not allowed", we'd just skip that x in lookup and move to the outer scope.
That, or have each variable declaration introduce a new scope that covers the rest of the surrounding block.
(We'd probably still want to disallow declaring the same variable more than once in the same scope, although nothing prevents allowing that, with each introducing a new scope.)

@lrhn lrhn transferred this issue from dart-lang/sdk May 14, 2024
@lrhn lrhn added the request Requests to resolve a particular developer problem label May 14, 2024
@mateusfccp
Copy link
Contributor

(We'd probably still want to disallow declaring the same variable more than once in the same scope, although nothing prevents allowing that, with each introducing a new scope.)

Basically, #3322, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

4 participants