-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Rust: Small type inference tweaks #19038
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
210b4db
Rust: Encapsulate type parameter decoding/encoding
paldepind 422d9e1
Rust: Minor refactoring of type inference
paldepind 4c3768f
Rust: Add comments for type inference
paldepind 1b7f4e4
Rust: Add type inference tests and rename modules
paldepind 75355e9
Rust: Revert conjunct reorder
paldepind 81b28df
Merge branch 'main' into rust-type-inference-tweaks
paldepind 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,69 @@ | ||
| mod m1 { | ||
| mod field_access { | ||
| #[derive(Debug)] | ||
| struct S; | ||
|
|
||
| #[derive(Debug)] | ||
| struct MyThing { | ||
| a: S, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| enum MyOption<T> { | ||
| MyNone(), | ||
| MySome(T), | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct GenericThing<A> { | ||
| a: A, | ||
| } | ||
|
|
||
| struct OptionS { | ||
| a: MyOption<S>, | ||
| } | ||
|
|
||
| fn simple_field_access() { | ||
| let x = MyThing { a: S }; | ||
| println!("{:?}", x.a); | ||
| } | ||
|
|
||
| fn generic_field_access() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do all the expressions in this test have correctly inferred types (I didn't bother to check the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe so, yes :) |
||
| // Explicit type argument | ||
| let x = GenericThing::<S> { a: S }; | ||
| println!("{:?}", x.a); | ||
|
|
||
| // Implicit type argument | ||
| let y = GenericThing { a: S }; | ||
| println!("{:?}", x.a); | ||
|
|
||
| // The type of the field `a` can only be infered from the concrete type | ||
| // in the struct declaration. | ||
| let x = OptionS { | ||
| a: MyOption::MyNone(), | ||
| }; | ||
| println!("{:?}", x.a); | ||
|
|
||
| // The type of the field `a` can only be infered from the type argument | ||
| let x = GenericThing::<MyOption<S>> { | ||
| a: MyOption::MyNone(), | ||
| }; | ||
| println!("{:?}", x.a); | ||
|
|
||
| let mut x = GenericThing { | ||
| a: MyOption::MyNone(), | ||
| }; | ||
| // Only after this access can we infer the type parameter of `x` | ||
| let a: MyOption<S> = x.a; | ||
| println!("{:?}", a); | ||
| } | ||
|
|
||
| pub fn f() { | ||
| simple_field_access(); | ||
| generic_field_access(); | ||
| } | ||
| } | ||
|
|
||
| mod method_impl { | ||
| pub struct Foo {} | ||
|
|
||
| impl Foo { | ||
|
|
@@ -25,7 +90,7 @@ mod m1 { | |
| } | ||
| } | ||
|
|
||
| mod m2 { | ||
| mod method_non_parametric_impl { | ||
| #[derive(Debug)] | ||
| struct MyThing<A> { | ||
| a: A, | ||
|
|
@@ -58,6 +123,10 @@ mod m2 { | |
| let x = MyThing { a: S1 }; | ||
| let y = MyThing { a: S2 }; | ||
|
|
||
| // simple field access | ||
| println!("{:?}", x.a); | ||
| println!("{:?}", y.a); | ||
|
|
||
| println!("{:?}", x.m1()); // missing call target | ||
| println!("{:?}", y.m1().a); // missing call target | ||
|
|
||
|
|
@@ -69,7 +138,7 @@ mod m2 { | |
| } | ||
| } | ||
|
|
||
| mod m3 { | ||
| mod method_non_parametric_trait_impl { | ||
| #[derive(Debug)] | ||
| struct MyThing<A> { | ||
| a: A, | ||
|
|
@@ -122,7 +191,7 @@ mod m3 { | |
| } | ||
| } | ||
|
|
||
| mod m4 { | ||
| mod function_trait_bounds { | ||
| #[derive(Debug)] | ||
| struct MyThing<A> { | ||
| a: A, | ||
|
|
@@ -175,7 +244,7 @@ mod m4 { | |
| } | ||
| } | ||
|
|
||
| mod m5 { | ||
| mod trait_associated_type { | ||
| trait MyTrait { | ||
| type AssociatedType; | ||
|
|
||
|
|
@@ -210,7 +279,7 @@ mod m5 { | |
| } | ||
| } | ||
|
|
||
| mod m6 { | ||
| mod generic_enum { | ||
| #[derive(Debug)] | ||
| enum MyEnum<A> { | ||
| C1(A), | ||
|
|
@@ -240,7 +309,7 @@ mod m6 { | |
| } | ||
| } | ||
|
|
||
| mod m7 { | ||
| mod method_supertraits { | ||
| #[derive(Debug)] | ||
| struct MyThing<A> { | ||
| a: A, | ||
|
|
@@ -325,7 +394,7 @@ mod m7 { | |
| } | ||
| } | ||
|
|
||
| mod m8 { | ||
| mod function_trait_bounds_2 { | ||
| use std::convert::From; | ||
| use std::fmt::Debug; | ||
|
|
||
|
|
@@ -374,7 +443,7 @@ mod m8 { | |
| } | ||
| } | ||
|
|
||
| mod m9 { | ||
| mod option_methods { | ||
| #[derive(Debug)] | ||
| enum MyOption<T> { | ||
| MyNone(), | ||
|
|
@@ -456,7 +525,7 @@ mod m9 { | |
| } | ||
| } | ||
|
|
||
| mod m10 { | ||
| mod method_call_type_conversion { | ||
|
|
||
| #[derive(Debug, Copy, Clone)] | ||
| struct S<T>(T); | ||
|
|
@@ -508,7 +577,7 @@ mod m10 { | |
| } | ||
| } | ||
|
|
||
| mod m11 { | ||
| mod trait_implicit_self_borrow { | ||
| trait MyTrait { | ||
| fn foo(&self) -> &Self; | ||
|
|
||
|
|
@@ -531,7 +600,7 @@ mod m11 { | |
| } | ||
| } | ||
|
|
||
| mod m12 { | ||
| mod implicit_self_borrow { | ||
| struct S; | ||
|
|
||
| struct MyStruct<T>(T); | ||
|
|
@@ -548,7 +617,7 @@ mod m12 { | |
| } | ||
| } | ||
|
|
||
| mod m13 { | ||
| mod borrowed_typed { | ||
| struct S; | ||
|
|
||
| impl S { | ||
|
|
@@ -578,18 +647,19 @@ mod m13 { | |
| } | ||
|
|
||
| fn main() { | ||
| m1::f(); | ||
| m1::g(m1::Foo {}, m1::Foo {}); | ||
| m2::f(); | ||
| m3::f(); | ||
| m4::f(); | ||
| m5::f(); | ||
| m6::f(); | ||
| m7::f(); | ||
| m8::f(); | ||
| m9::f(); | ||
| m10::f(); | ||
| m11::f(); | ||
| m12::f(); | ||
| m13::f(); | ||
| field_access::f(); | ||
| method_impl::f(); | ||
| method_impl::g(method_impl::Foo {}, method_impl::Foo {}); | ||
| method_non_parametric_impl::f(); | ||
| method_non_parametric_trait_impl::f(); | ||
| function_trait_bounds::f(); | ||
| trait_associated_type::f(); | ||
| generic_enum::f(); | ||
| method_supertraits::f(); | ||
| function_trait_bounds_2::f(); | ||
| option_methods::f(); | ||
| method_call_type_conversion::f(); | ||
| trait_implicit_self_borrow::f(); | ||
| implicit_self_borrow::f(); | ||
| borrowed_typed::f(); | ||
| } | ||
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.
Note, this line calls
getResolvedFunctionand casts toAstNode. The next line callsgetResolvedFunctionand calls toStruct. Seems like the later will always be included in the former.