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 fails to respect certain "is" checks #11080

Closed
nex3 opened this issue Jun 4, 2013 · 11 comments
Closed

Analyzer fails to respect certain "is" checks #11080

nex3 opened this issue Jun 4, 2013 · 11 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report
Milestone

Comments

@nex3
Copy link
Member

nex3 commented Jun 4, 2013

Consider the following code:

    import "dart:typed_data";

    void doIt(List list) {
      if (list is TypedData) print(list.buffer);
    }

Running the analyzer on this produces the following error:

    [warning] There is no such getter 'buffer' in 'List'

despite the fact that "list" must be an instance of TypedData in the "if" block, and TypedData defines a "buffer" getter.

@kevmoo
Copy link
Member

kevmoo commented Jun 4, 2013

Related to (duplicate of?) https://code.google.com/p/dart/issues/detail?id=6397

@bwilkerson
Copy link
Member

Added this to the M6 milestone.
Removed Priority-Unassigned label.
Added Priority-Medium label.

@bwilkerson
Copy link
Member

Unfortunately, it has been decided that dartanalyzer and the editor cannot use type inference to eliminate errors and warnings. The only way to get rid of this error would be to write

  print((list as TypedData).buffer);

even though the cast seems redundant.


Set owner to @bwilkerson.
Added WontFix label.

@nex3
Copy link
Member Author

nex3 commented Nov 16, 2013

I thought this had been changed in the spec, but it still seems not to work. Was I misinformed?

@bwilkerson
Copy link
Member

I thought this had been changed in the spec, but it still seems not to work. Was I misinformed?

It was changed in the specification, but only for a very limited set of cases. The specification reads:

    Let v be a local variable or a formal parameter. An is-expression of the form v is T shows
    that v has type T iff T is a more specific than the type S of the expression v and both
    T != dynamic and S != dynamic.

So in this case, the parameter 'list' would be shown to have type 'TypedData' iff 'TypedData' is more specific than 'List'. Unfortunately, there is no relationship between 'TypedData' and 'List', so we cannot use that information for the purposes of suppressing warnings. If 'TypedData' implemented 'List', then this would work the way you expect.

@nex3 nex3 added Type-Defect area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Nov 16, 2013
@nex3 nex3 added this to the M6 milestone Nov 16, 2013
@kevmoo kevmoo added closed-not-planned Closed as we don't intend to take action on the reported issue and removed resolution-wont_fix labels Mar 1, 2016
@Hixie
Copy link
Contributor

Hixie commented Sep 19, 2016

I don't understand this limitation. If you enter a code block that's gated by an if (foo is Bar), then it makes no sense to me that the analyzer wouldn't assume that isn't that block, foo is a Bar, irrespective of any other types that it might think foo is as well.

@bwilkerson
Copy link
Member

Consider the following code:

abstract class A {
  void mA();
}

abstract class B {
  void mB();
}

void f(A p) {
  if (p is B) {
    p.mA();
    p.mB();
  }
}

In the absence of intersection types you can choose one of two "wrong" semantics. Within the body of the if, either p has type A or p has type B. Either way you end up with a spurious error message.

The original authors chose one of the two options, but both are bad. I'm not sure which would result if fewer spurious error messages in practice. Feel free to re-open this case if you want the language team to consider changing this behavior.

@Hixie
Copy link
Contributor

Hixie commented Sep 20, 2016

I think the analyzer should assume that in that block, p is both A and B.

@Hixie
Copy link
Contributor

Hixie commented Sep 20, 2016

(i don't have the access to reopen bugs in the dart sdk repo :-) )

@bwilkerson bwilkerson reopened this Sep 20, 2016
@bwilkerson bwilkerson added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). and removed area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. closed-not-planned Closed as we don't intend to take action on the reported issue labels Sep 20, 2016
@bwilkerson
Copy link
Member

We tried adding union types behind the scenes awhile back and it turned out to be so confusing to users that we ended up backing all that out. I'm concerned that intersection types would be the same. In any case, this behavior is currently part of the specification, so I'm turning this into a language issue.

@lrhn
Copy link
Member

lrhn commented Jun 25, 2018

Looks like a variant of an "improve type promotion" request (#25565)

@lrhn lrhn closed this as completed Jun 25, 2018
@lrhn lrhn added the closed-duplicate Closed in favor of an existing report label Jun 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report
Projects
None yet
Development

No branches or pull requests

5 participants