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

Analyzer missing error for 'this' access in a constructor assert #36779

Closed
DanTup opened this issue Apr 27, 2019 · 4 comments
Closed

Analyzer missing error for 'this' access in a constructor assert #36779

DanTup opened this issue Apr 27, 2019 · 4 comments
Assignees
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@DanTup
Copy link
Collaborator

DanTup commented Apr 27, 2019

After some refactoring I ended up with code like this:

class Danny {
  int length;
  Danny(String str)
      : length = str.length,
        assert(length != null);
}

I forgot to update the assert to check str, and although this code gives no analysis-time warnings, it fails to run with this compile error:

Compiler message:

lib/src/painter.dart:277:16: Error: Can't access 'this' in a field initializer to read 'length'.
        assert(length != null);
               ^^^^^^

I think there could/should be an analysis error for this if it's not valid.

@devoncarew
Copy link
Member

@stereotype441, is this a an issue in the analyzer (for not warning) or in the CFE (for the runtime error)?

@stereotype441
Copy link
Member

I believe the error is correct, and the bug is in the analyzer.

@stereotype441 stereotype441 added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Apr 29, 2019
@lrhn
Copy link
Member

lrhn commented Apr 30, 2019

The bug is in the analyzer.
The length in the assert refers to the instance field, making it equivalent to this.length, and you cannot access this (or instance fields) in initializer lists.

The places where you can access instance methods, or this at all, are:

  • Instance member bodies.
  • Constructor bodies.
    (The this in initializer lists or initializing formals are simply syntactic forms, they do not refer to the object).

If this occurs as an expression anywhere else, it is an error. If an implicit access to an instance field occurs anywhere else, it is an error.
(The C(this.length) : assert(length != 0); constructor assert does not reference the instance field, it references a new final variable with the same name introduced by the initilaizing formal, no this involved)

@devoncarew devoncarew changed the title Compiler "Can't access 'this' in a field initializer" when building, but not at analysis time Analyzer missing error for 'this' access in a constructor assert Apr 30, 2019
@stereotype441 stereotype441 self-assigned this May 3, 2019
stereotype441 added a commit to stereotype441/flutter that referenced this issue May 4, 2019
This erroneous code is currently slipping through loopholes in both
the front end (due to dart-lang/sdk#36842)
and the analyzer (due to
dart-lang/sdk#36779).  Before we fix those
bugs we need to stop inadvertently taking advantage of them in the
Flutter codebase.
@srawlins srawlins added the analyzer-spec Issues with the analyzer's implementation of the language spec label Jun 16, 2020
@lrhn
Copy link
Member

lrhn commented May 17, 2022

Appears to be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants