Sdk: Disabling implicit-dynamic doesn't work with redirected factory constructors

Created on 21 Feb 2020  路  4Comments  路  Source: dart-lang/sdk

Consider this code:

abstract class Example<T> {
  factory Example() = _Example;
}

class _Example<T> implements Example<T> {
}

The line factory Example() = _Example; is actually equivalent to factory Example() = _Example<dynamic>

But the tools don't pick it up (at least VScode doesn't complain).
I would expect either the generic argument to be inferred or the tooling to warn about an implicit-dynamic.

analyzer-hint area-analyzer

Most helpful comment

@rrousselGit wrote:

I would expect either the generic argument to be inferred or the tooling to warn about an implicit-dynamic.

https://github.com/dart-lang/language/issues/366 clarified that the inference mentioned here is the correct behavior, and https://github.com/dart-lang/sdk/commit/3b93b85aeff4d0beb8b262044a12dff1af56df16 implemented this in the common front end (it was already implemented in the analyzer before the issue came up).

dartpad.dev does not yet run a sufficiently new version of the front end to have this fix, but it is working in fresh versions of the tools.

When the fix is available, the given example should not give rise to any diagnostic messages and the reported behavior is correct.

So, @rrouselGit, I'll close this issue. Please create a new one as needed if some elements of this issue still haven't been resolved.

@beauxq wrote:

These are not giving me any error like I want them to.

The given type annotations are known as raw types (which are basically types of the form C where C is a generic class, that is, the actual type arguments are missing). There is no kind of inference which will provide those missing type arguments. The mechanism which is used is instantiation to bound, and in the case where type parameters have no bound the resulting actual type argument is dynamic. In some other cases instantiation to bound may yield a result that uses dynamic as a type argument, nested one or more levels.

It makes a lot of sense to flag these implicit type arguments that are or contain dynamic along with other implicitly introduced occurrences of the type dynamic. I created https://github.com/dart-lang/linter/issues/2181 in order to request a lint for these cases.

All 4 comments

Is this the same problem as the problem I just found?
I set in analysis_options

analyzer:
  strong-mode:
    implicit-dynamic: false

and I have the code

List a;
Map b;
Set c;

These are not giving me any error like I want them to.
They all have dynamic in their typing when I didn't explicitly say dynamic.

@rrousselGit wrote:

I would expect either the generic argument to be inferred or the tooling to warn about an implicit-dynamic.

https://github.com/dart-lang/language/issues/366 clarified that the inference mentioned here is the correct behavior, and https://github.com/dart-lang/sdk/commit/3b93b85aeff4d0beb8b262044a12dff1af56df16 implemented this in the common front end (it was already implemented in the analyzer before the issue came up).

dartpad.dev does not yet run a sufficiently new version of the front end to have this fix, but it is working in fresh versions of the tools.

When the fix is available, the given example should not give rise to any diagnostic messages and the reported behavior is correct.

So, @rrouselGit, I'll close this issue. Please create a new one as needed if some elements of this issue still haven't been resolved.

@beauxq wrote:

These are not giving me any error like I want them to.

The given type annotations are known as raw types (which are basically types of the form C where C is a generic class, that is, the actual type arguments are missing). There is no kind of inference which will provide those missing type arguments. The mechanism which is used is instantiation to bound, and in the case where type parameters have no bound the resulting actual type argument is dynamic. In some other cases instantiation to bound may yield a result that uses dynamic as a type argument, nested one or more levels.

It makes a lot of sense to flag these implicit type arguments that are or contain dynamic along with other implicitly introduced occurrences of the type dynamic. I created https://github.com/dart-lang/linter/issues/2181 in order to request a lint for these cases.

Epic, thanks a lot!

Turns out that there is a way to request this kind of check already: Put strict-raw-types: true in analysis_options.yaml. Thanks to @srawlins for the heads up about this feature!

Was this page helpful?
0 / 5 - 0 ratings