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 problem with map initializers #15565

Closed
jtmcdole opened this issue Dec 10, 2013 · 8 comments
Closed

Analyzer problem with map initializers #15565

jtmcdole opened this issue Dec 10, 2013 · 8 comments
Assignees
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
Milestone

Comments

@jtmcdole
Copy link
Contributor

In the following example, there is an error for a closure (key 'bar') that accesses a member of the class if its directly assigned in the initialization. In any of the other locations (keys 'bop' and 'baz') there is no report. I think this is a problem since its surprising / unexpected.

class Foo {
  String bar = "hello world";
  Map foo = {
    'bar': () {
        var _bar = bar;
                   ^^^
        return _bar;
    'bop': _foo
  };

  _foo() {
    var _bar = bar;
    return _bar;
  }

  Foo() {
    foo['baz'] = () {
      var _bar = bar;
      return _bar;
    };
  }
}

Error message: Only static members can be accessed in initializers.

@bwilkerson
Copy link
Member

Added this to the 1.1 milestone.
Removed Priority-Unassigned label.
Added Priority-Medium, Area-Analyzer, Triaged labels.

@bwilkerson
Copy link
Member

The specification (section 12.11 on This expressions) is unclear about whether this example is allowed or not, so I'm passing this issue on to the language committee.


Removed this from the 1.1 milestone.
Removed Priority-Medium, Area-Analyzer labels.
Added Priority-Unassigned, Area-Language labels.

@gbracha
Copy link
Contributor

gbracha commented Aug 27, 2014

The example shown has many errors. I assume the intent was:

class Foo {
  String bar = "hello world";
  Map foo = {
    'bar': () {
        var _bar = bar;
        return _bar;
    },
    'bop': _foo
  };

  _foo() {
    var _bar = bar;
    return _bar;
  }

  Foo() {
    foo['baz'] = () {
      var _bar = bar;
      return _bar;
    };
  }
}

The issue is that the initializer for the field foo contains a reference to the field bar. The specification says that it is a compile time error if 'this' appears in the initializer of an instance variable. The intent is to cover both explicit and implicit uses of 'this'. The use of _bar is defined to mean this._bar (see the section on identifier reference), which is getter invocation of the implicitly generated getter for the field _bar. I'll clarify that point in the next draft.

I'm sending this back to the analyzer; I think the only concern is that only _bar gets flagged but _foo does not (at least in the editor). Note that _foo is a closurization of the _foo method, and again involves this implicitly and is an error. The case of 'baz" is totally different since it occurs in a constructor body, where 'this' is allowed.


Removed Area-Language label.
Added Area-Analyzer label.

@bwilkerson
Copy link
Member

Removed Priority-Unassigned label.
Added Priority-Medium label.

@stereotype441
Copy link
Member

Set owner to @stereotype441.
Added Started label.

@stereotype441
Copy link
Member

Fix submitted for review: https://codereview.chromium.org/702623002/

@stereotype441
Copy link
Member

Fixed in revision 41478.


Added Fixed label.

@clayberg
Copy link

Added this to the 1.8 milestone.

@jtmcdole jtmcdole added Type-Defect area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Nov 28, 2014
@jtmcdole jtmcdole added this to the 1.8 milestone Nov 28, 2014
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
Projects
None yet
Development

No branches or pull requests

5 participants