-
Notifications
You must be signed in to change notification settings - Fork 28
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
#2976. Add type inference tests. Part 2 #3005
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
LanguageFeatures/Static-access-shorthand/grammar_A03_t02.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion We also add . to the tokens that an expression statement cannot | ||
/// start with. This doesn't affect starting with a double literal like `.42`, | ||
/// since that's a different token than a single `.`. | ||
/// | ||
/// @description Checks that it is a compile-time error if an expression | ||
/// statement is started with `.`. | ||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=enum-shorthands | ||
|
||
void foo() {} | ||
|
||
class C {} | ||
|
||
enum E {e0;} | ||
|
||
main() { | ||
.new; | ||
//^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
.new(); | ||
//^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
.C(); | ||
//^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
.foo(); | ||
//^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
.e0; | ||
//^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} |
71 changes: 71 additions & 0 deletions
71
LanguageFeatures/Static-access-shorthand/semantics_A05_t01.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion Dart semantics, static and dynamic, do not follow the grammar | ||
/// precisely. For example, a static member invocation expression of the form | ||
/// `C.id<T1>(e2)` is treated as an atomic entity for type inference (and | ||
/// runtime semantics). It’s not a combination of doing a `C.id` tear-off, then | ||
/// a `<T1>` instantiation and then an `(e2)` invocation. The context type of | ||
/// that entire expression is used throughout the inference, where | ||
/// `(e1.id<T1>)(e2)` has `(e1.id<T1>)` in a position where it has no context | ||
/// type. | ||
/// | ||
/// Because of that, the specification of the static and runtime semantics of | ||
/// the new constructs needs to address all the forms `.id`, `.id<typeArgs>`, | ||
/// `.id(args)`, `.id<typeArgs>(args)`, `.new` or `.new(args)`. | ||
/// ... | ||
/// The general rule is that any of the expression forms above, starting with | ||
/// `.id`, are treated exactly as if they were preceded by a fresh prefixed | ||
/// identifier `_p.C` which denotes the declaration of the type of the context | ||
/// type scheme of the entire `<staticMemberShorthand>`. | ||
/// | ||
/// @description Checks that a named or unnamed constructor can be torn-off | ||
/// using a shorthand syntax. | ||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=enum-shorthands | ||
|
||
import '../../Utils/expect.dart'; | ||
|
||
class C<T> { | ||
T t; | ||
C(this.t); | ||
C.id(this.t); | ||
} | ||
|
||
extension type ET<T>(T t) { | ||
ET.id(this.t); | ||
} | ||
|
||
main() { | ||
Object? o = C<int>(0); | ||
if (o is C<int>) { | ||
o = .new; | ||
if (o is Function) { | ||
Expect.equals(1, o(1).t); | ||
} | ||
} | ||
o = C(0); | ||
if (o is C) { | ||
o = .id; | ||
if (o is Function) { | ||
Expect.equals(2, o(2).t); | ||
} | ||
} | ||
|
||
o = ET<int>(0); | ||
if (o is ET) { | ||
o = .new; | ||
if (o is Function) { | ||
Expect.equals(3, o(3)); | ||
} | ||
} | ||
o = ET<int>(0); | ||
if (o is ET) { | ||
o = .id; | ||
if (o is Function) { | ||
Expect.equals(4, o(4)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
LanguageFeatures/Static-access-shorthand/type_inference_A06_t02.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion Expressions of the forms `.new<typeArgs>(args)` or | ||
/// `.new<typeArgs>` (as a prefix of a `<staticMemberShorthand> <selector>*` | ||
/// production, or the entire chain) are compile-time errors, just like the | ||
/// corresponding `T.new<typeArgs>(args)` and `T.new<typeArgs>` already are, | ||
/// whether used as instantiated tear-off or invoked. | ||
/// | ||
/// @description Checks that it is a compile-time error to use a shorthand | ||
/// expression of the form `.new<typeArgs>`. | ||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=enum-shorthands | ||
|
||
class C<T> { | ||
C(); | ||
} | ||
|
||
extension type ET<T>(T v) {} | ||
|
||
main() { | ||
Object? o = C(); | ||
if (o is C) { | ||
o = .new<int>; | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
|
||
o = ET(0); | ||
if (o is ET) { | ||
o = .new<int>; | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
LanguageFeatures/Static-access-shorthand/type_inference_A07_t01.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion The invocation of a constructor is not using an instantiated | ||
/// type, it’s behaving as if the constructor was preceded by a raw type, which | ||
/// type inference should then infer type arguments for. Doing | ||
/// `List<int> l = .filled(10, 10);` works like doing | ||
/// `List<int> l = List.filled(10, 10);`, and it is the following downwards | ||
/// inference with context type `List<int>` that makes it into | ||
/// `List<int>.filled(10, 10);`. This distinction matters for something like: | ||
/// ``` | ||
/// List<String> l = | ||
/// .generate(10, (int i) => i + 1).map((x) => x.toRadixString(16)).toList(); | ||
/// ``` | ||
/// which is equivalent to inserting `List` in front of `.generate`, which will | ||
/// then be inferred as `List<int>`. In most normal use cases it doesn’t matter, | ||
/// because the context type will fill in the missing type variables, but if the | ||
/// construction is followed by more selectors, it loses that context type. | ||
/// | ||
/// @description Checks that it is a compile-time error to use a shorthand | ||
/// expression if additional selectors strip its context type. | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=enum-shorthands | ||
|
||
class C<T> { | ||
T value; | ||
C(this.value); | ||
C.id(this.value); | ||
|
||
C<int> toInt(int v) => C<int>(v); | ||
} | ||
|
||
extension type ET<T>(T v) { | ||
ET.id(this.v); | ||
|
||
ET<int> toInt(int v) => ET<int>(v); | ||
} | ||
|
||
main() { | ||
C<int> c1 = .new("String").toInt(0); | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
C<int> c2 = .id("String").toInt(0); | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
ET<int> et1 = .new("String").toInt(0); | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
ET<int> et2 = .id("String").toInt(0); | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
LanguageFeatures/Static-access-shorthand/type_inference_A07_t02.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion The invocation of a constructor is not using an instantiated | ||
/// type, it’s behaving as if the constructor was preceded by a raw type, which | ||
/// type inference should then infer type arguments for. Doing | ||
/// `List<int> l = .filled(10, 10);` works like doing | ||
/// `List<int> l = List.filled(10, 10);`, and it is the following downwards | ||
/// inference with context type `List<int>` that makes it into | ||
/// `List<int>.filled(10, 10);`. This distinction matters for something like: | ||
/// ``` | ||
/// List<String> l = | ||
/// .generate(10, (int i) => i + 1).map((x) => x.toRadixString(16)).toList(); | ||
/// ``` | ||
/// which is equivalent to inserting `List` in front of `.generate`, which will | ||
/// then be inferred as `List<int>`. In most normal use cases it doesn’t matter, | ||
/// because the context type will fill in the missing type variables, but if the | ||
/// construction is followed by more selectors, it loses that context type. | ||
/// | ||
/// @description Checks that it is a compile-time error to use a shorthand | ||
/// expression if additional selectors strip its context type. | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=enum-shorthands | ||
|
||
main() { | ||
List<String> l = | ||
.generate(10, (int i) => i + 1).map((x) => x.toRadixString(16)).toList(); | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
num n = .parse("42").abs(); | ||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
int i = (.parse("42")).abs(); | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
|
||
int v = .parse("42") + 1; | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// ^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
} |
55 changes: 55 additions & 0 deletions
55
LanguageFeatures/Static-access-shorthand/type_inference_A08_t01.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion Since `!` does propagate a context, | ||
/// `int x = (.tryParse(input))!;` does work, with a context type scheme of | ||
/// `int?`, which is enough to allow `.tryParse`. Same for | ||
/// `int x = .tryParse(input) ?? 0;` which gives the first operand the context | ||
/// type `int?`. | ||
/// | ||
/// @description Checks that if shorthand expression is followed by `!` then its | ||
/// original context type is preserved. | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=enum-shorthands | ||
|
||
import '../../Utils/expect.dart'; | ||
|
||
class C { | ||
int value; | ||
C(this.value); | ||
|
||
static C? get id => C(0); | ||
} | ||
|
||
mixin M on C { | ||
static M? get id => MC(1); | ||
} | ||
class MC = C with M; | ||
|
||
enum E { | ||
e0(2); | ||
final int value; | ||
const E(this.value); | ||
|
||
static E? get id => E.e0; | ||
} | ||
|
||
extension type ET(int v) { | ||
static ET get id => ET(3); | ||
} | ||
|
||
main() { | ||
C c = .id!; | ||
Expect.equals(0, c.value); | ||
|
||
M m = .id!; | ||
Expect.equals(1, m.value); | ||
|
||
E e = .id!; | ||
Expect.equals(2, e.value); | ||
|
||
ET et = .id!; | ||
Expect.equals(3, et.v); | ||
} |
55 changes: 55 additions & 0 deletions
55
LanguageFeatures/Static-access-shorthand/type_inference_A08_t02.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// @assertion Since `!` does propagate a context, | ||
/// `int x = (.tryParse(input))!;` does work, with a context type scheme of | ||
/// `int?`, which is enough to allow `.tryParse`. Same for | ||
/// `int x = .tryParse(input) ?? 0;` which gives the first operand the context | ||
/// type `int?`. | ||
/// | ||
/// @description Checks that if shorthand expression is followed by `??` then | ||
/// its original context type is preserved. | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=enum-shorthands | ||
|
||
import '../../Utils/expect.dart'; | ||
|
||
class C { | ||
int value; | ||
C(this.value); | ||
|
||
static C? get id => C(0); | ||
} | ||
|
||
mixin M on C { | ||
static M? get id => MC(1); | ||
} | ||
class MC = C with M; | ||
|
||
enum E { | ||
e0(2), e1(-1); | ||
final int value; | ||
const E(this.value); | ||
|
||
static E? get id => E.e0; | ||
} | ||
|
||
extension type ET(int v) { | ||
static ET get id => ET(3); | ||
} | ||
|
||
main() { | ||
C c = .id ?? C(-1); | ||
Expect.equals(0, c.value); | ||
|
||
M m = .id ?? MC(-1); | ||
Expect.equals(1, m.value); | ||
|
||
E e = .id ?? E.e1; | ||
Expect.equals(2, e.value); | ||
|
||
ET et = .id ?? ET(-1); | ||
Expect.equals(3, et.v); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I'm not sure
dynamic
as a context type schema is equivalent to_
. But we should have the error anyway, and it's a delicate question, so let's have it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Proposal states that a context type schema in this case is
_
.https://github.com/dart-lang/language/blob/1b0e339c52e73acc875bd68451b46cef9391f028/working/3616%20-%20enum%20value%20shorthand/proposal-simple-lrhn.md?plain=1#L306
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting... I can't see that this has been specified anywhere, but the choice of context type schema in every given situation is highly underspecified anyway.
One source of confusion is that the implemented type inference algorithm chooses
dynamic
in a number of cases where the specification in inference.md says that the inferred result is the greatest closure. I created dart-lang/language#4197 to shed some light on that.If we assume that this is fixed then I'd expect the following behavior:
This should illustrate that the context type schema
_
and the context typedynamic
is not the same thing. So we still need some clarifications, but I'd maintain that it isn't safe to assume that they are the same thing.