How to turn dynamic type into Object in dartanalyzer

21 views
Skip to first unread message

Fabio Strocco

unread,
Jan 27, 2015, 11:20:05 AM1/27/15
to compil...@dartlang.org
Hello, I am trying to the give the "Object" semantics to all dynamic types (dynamic annotation, use of var, missing annotation) in dartanalyzer, but I don't know how to do.

For example,

class C {
  m1(x) {
    x.foo;
  }

  void m2() {
    m1(10).doSomething();
  }
}

Despite this program does not produce any type warning I would like the analyzer say something like

[warning] foo is not a property of the class Object
[warning] doSomething is not a method in the class Object

In essence, I would like to turn all the missing type annotation, dynamic annotations and var into Object in the AST nodes, or somehow instruct the analyzer that x has type Object and m1 has Object return type.
Is there an easy way to do that?

Thanks in advance.


Seth Ladd

unread,
Jan 27, 2015, 11:23:16 AM1/27/15
to Fabio Strocco, analyzer...@dartlang.org, compil...@dartlang.org
+ analyzer-discuss@

To unsubscribe from this group and stop receiving emails from it, send an email to compiler-dev...@dartlang.org.

Brian Wilkerson

unread,
Jan 27, 2015, 11:33:47 AM1/27/15
to Fabio Strocco, analyzer...@dartlang.org, compil...@dartlang.org
Fabio,

I would like to turn all the missing type annotation, dynamic annotations and var into Object in the AST nodes, or somehow instruct the analyzer that x has type Object and m1 has Object return type.

I'd be interested in hearing why you want to do this. It's quite contrary to the philosophy of Dart, and it seems like you'd get lots of false positives, but I assume you have a use case in mind.

Is there an easy way to do that?

I'm not sure what you consider to be easy :-), but if you're asking, for example, whether there's a command-line option to do this, the answer is 'no'.

But if you're talking about making changes to the analyzer code, the easiest way I can think of is to modify the AST structure after the parser creates it and before the resolver starts generating those kinds of errors and warnings. I suspect that you should be able to modify the AST structure to make it look like the use had used 'Object' anywhere a type could be specified but was either omitted or given as 'dynamic'.

On the other hand, if your goal is to identify places where no type information was provided, you could get that information without modifying the AST structure and add it to the errors and warnings (and you could associate the messages with the declaration site rather than the use site).

Brian

Fabio Strocco

unread,
Jan 27, 2015, 11:56:44 AM1/27/15
to compil...@dartlang.org, fabios...@gmail.com, analyzer...@dartlang.org
Thank you very much for the quick reply. I was having hard problems to localize the exact phase to modify the AST and for some node it seems there is no setter for "type". Anyway I found out that it works by changing the TypeProvider.dynamicType so that it returns _objectType instead of _dynamicType.

I am not against dynamic and I am not trying to use such an implementation for a real program.
My goal is to find out how many missing type annotations (I can already count the number of missing type annotations in a source) can be easily replaced by Object (or better, by simple types like String, int, etc.)
Reply all
Reply to author
Forward
0 new messages