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

Exact types #33307

Open
eernstg opened this issue Jun 1, 2018 · 1 comment
Open

Exact types #33307

eernstg opened this issue Jun 1, 2018 · 1 comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).

Comments

@eernstg
Copy link
Member

eernstg commented Jun 1, 2018

This issue is intended to clarify the approach to exact types in Dart.

An expression e has an exact type if it is statically known that the result v yielded from an evaluation of e will have a specific type T, and there is no proper subtype S of T such that v has type S. Examples are literals (42, true, <int>[2, 3]) instance creations denoting generative constructors (C("Hello, world!")), and constant expressions in general.

Exact types have been on the table many times; they play a role in the following Dart SDK issues, at least:

The language specification does not take this concept into account, there are no "exact types" and hence no expressions are considered to have such a type. However, the actual dynamic semantics ensures that certain expressions (such as the ones mentioned above) will indeed satisfy the requirement associated with an exact type, so we already have the dynamic semantics in place for using this concept.

We could handle exact types outside the language: A lint could characterize any set of expressions as having an exact type (along with a careful argument that each of those expressions will actually evaluate to a value of exactly that type). We could also introduce the notion of exact types in the language specification (based on a similar careful soundness argument) and make exact types part of the language.

In both cases, we could then flag situations (as a lint violation respectively a compile-time error) where a dynamic type check will be performed, but it is guaranteed to fail at run time because the value under scrutiny will never have the requested type (e.g., List<int> xs = <num>[];).

Note that List<int> xs = <num>[]; is flagged as an error by the analyzer already today, which illustrates that the concept of exact types is already being used in some sense.

The purpose of this issue is to handle the discussion about whether to introduce such a concept, at which level to do it (lint/language), how to define the concept itself in more detail, and exactly which situations to flag as wrong with some kind of diagnostic message.

@eernstg eernstg added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Jun 1, 2018
@lrhn
Copy link
Member

lrhn commented Jun 1, 2018

The concept of exact types are part of strong mode, so they are, de-facto, introduced in the Dart 2 language.

Specifying it is a separate issue. We could take the vague approach and say that tools may make anything that is a guaranteed run-time type error if evaluated into a compile-time error. That would leave the implementations open to improve their analyses over time.
Or we could make a definitive list of the expressions that have exact types, and just say that we don't allow implicit down-cast on those expressions. (We can also warn about explicit down-casts, but that should probably only be a warning).

dart-bot pushed a commit that referenced this issue Jan 11, 2019
The test exposes three problems:

- The disambiguation criterion in the type inference is incorrect, as
  it infers a map when the context is a LinkedHashSet. This will be
  fixed in an upcoming CL.

- Assigning a map literal to a LinkedHashMap produces a static error
  ("isn't of expected type") due to exact types. Rules for exact types
  are currently unspecified, and the language team has expressed some
  interest in allowing this case. See also
  #33307

- Type arguments for the map literal are not inferred from the context
  when the outer context type is a LinkedHashMap. This is consistent
  between the CFE and analyzer. If the LinkedHashMap case is to be
  supported, this must be fixed as well.

Change-Id: I1519c844dae1f599306446c992cfe4825e4f20d4
Reviewed-on: https://dart-review.googlesource.com/c/88726
Reviewed-by: Peter von der Ahé <[email protected]>
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).
Projects
None yet
Development

No branches or pull requests

2 participants