-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
fallback to resolve infer generics in type-changing-struct-update #102129
Closed
SparrowLii
wants to merge
4
commits into
rust-lang:master
from
SparrowLii:type-changing-struct-update
Closed
Changes from all commits
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
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
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
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
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
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 @@ | ||
use crate::infer::at::{At, ToTrace}; | ||
use crate::infer::sub::Sub; | ||
use crate::infer::{InferOk, InferResult}; | ||
use rustc_middle::ty; | ||
use rustc_middle::ty::relate::{relate_generic_arg, RelateResult, TypeRelation}; | ||
use rustc_middle::ty::{GenericArg, SubstsRef, Ty}; | ||
use std::iter; | ||
|
||
pub fn base_struct<'a, 'tcx>(at: At<'a, 'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, ()> { | ||
let trace = ToTrace::to_trace(at.infcx.tcx, at.cause, true, a, b); | ||
at.infcx.commit_if_ok(|_| { | ||
let mut fields = at.infcx.combine_fields(trace, at.param_env, at.define_opaque_types); | ||
let mut sub = Sub::new(&mut fields, true); | ||
base_struct_tys(&mut sub, a, b) | ||
.map(move |_| InferOk { value: (), obligations: fields.obligations }) | ||
}) | ||
} | ||
|
||
pub fn base_struct_tys<'tcx>( | ||
sub: &mut Sub<'_, '_, 'tcx>, | ||
a: Ty<'tcx>, | ||
b: Ty<'tcx>, | ||
) -> RelateResult<'tcx, ()> { | ||
match (a.kind(), b.kind()) { | ||
(&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs)) if a_def == b_def => { | ||
base_struct_substs(sub, a, a_substs, b_substs)?; | ||
Ok(()) | ||
} | ||
_ => bug!("not adt ty: {:?} and {:?}", a, b), | ||
} | ||
} | ||
|
||
fn base_struct_substs<'tcx>( | ||
sub: &mut Sub<'_, '_, 'tcx>, | ||
adt_ty: Ty<'tcx>, | ||
a_subst: SubstsRef<'tcx>, | ||
b_subst: SubstsRef<'tcx>, | ||
) -> RelateResult<'tcx, ()> { | ||
let tcx = sub.tcx(); | ||
let variances = tcx.variances_of(adt_ty.ty_adt_def().expect("not a adt ty!").did()); | ||
|
||
iter::zip(a_subst, b_subst).enumerate().for_each(|(i, (a, b))| { | ||
let _arg: RelateResult<'tcx, GenericArg<'tcx>> = | ||
relate_generic_arg(sub, variances, adt_ty, a, b, i).or_else(|_| Ok(b)); | ||
}); | ||
|
||
Ok(()) | ||
} |
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
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
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
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
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
23 changes: 5 additions & 18 deletions
23
src/test/ui/rfcs/rfc-2528-type-changing-struct-update/feature-gate.stderr
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 |
---|---|---|
@@ -1,22 +1,9 @@ | ||
error[E0658]: type changing struct updating is experimental | ||
--> $DIR/feature-gate.rs:22:11 | ||
| | ||
LL | ..m1 | ||
| ^^ | ||
| | ||
= note: see issue #86555 <https://github.com/rust-lang/rust/issues/86555> for more information | ||
= help: add `#![feature(type_changing_struct_update)]` to the crate attributes to enable | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/feature-gate.rs:22:11 | ||
| | ||
LL | ..m1 | ||
| ^^ expected struct `State2`, found struct `State1` | ||
--> $DIR/feature-gate.rs:21:16 | ||
| | ||
= note: expected struct `Machine<State2>` | ||
found struct `Machine<State1>` | ||
LL | state: State1, | ||
| ^^^^^^ expected struct `State2`, found struct `State1` | ||
|
||
error: aborting due to 2 previous errors | ||
error: aborting due to previous error | ||
|
||
Some errors have detailed explanations: E0308, E0658. | ||
For more information about an error, try `rustc --explain E0308`. | ||
For more information about this error, try `rustc --explain E0308`. |
40 changes: 40 additions & 0 deletions
40
src/test/ui/rfcs/rfc-2528-type-changing-struct-update/issue-101970.rs
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,40 @@ | ||
#![feature(type_changing_struct_update)] | ||
#![allow(incomplete_features)] | ||
|
||
#[derive(Default)] | ||
pub struct Foo<P, T> { | ||
pub t: T, | ||
pub v: P | ||
} | ||
|
||
impl<P: Default, T: Default> Foo<P, T> { | ||
pub fn new() -> Self { | ||
Foo { t: T::default(), v: P::default() } | ||
} | ||
|
||
pub fn d(t: T) -> Self { | ||
Foo { t, ..Default::default() } | ||
} | ||
|
||
pub fn o(t: T) -> Self { | ||
let d: Foo<_, _> = Foo::new(); | ||
Foo { t, ..d } | ||
} | ||
|
||
pub fn o2<P2: Default>(t: T, v: P2) -> (Foo<P, T>, Foo<P2, T>) { | ||
let d = Default::default(); | ||
let foo1 = Foo { t, ..d }; | ||
let foo2 = Foo { v, ..d }; | ||
(foo1, foo2) | ||
} | ||
|
||
pub fn o3<T2: Default>(t1: T, t2: T2) -> (Foo<P, T>, Foo<P, T2>) { | ||
let d = Default::default(); | ||
let foo1 = Foo { t: t1, ..d }; | ||
let foo2 = Foo { t: t2, ..d }; | ||
//~^ ERROR mismatched types [E0308] | ||
(foo1, foo2) | ||
} | ||
} | ||
|
||
fn main() {} |
20 changes: 20 additions & 0 deletions
20
src/test/ui/rfcs/rfc-2528-type-changing-struct-update/issue-101970.stderr
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,20 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-101970.rs:34:35 | ||
| | ||
LL | impl<P: Default, T: Default> Foo<P, T> { | ||
| - found type parameter | ||
... | ||
LL | pub fn o3<T2: Default>(t1: T, t2: T2) -> (Foo<P, T>, Foo<P, T2>) { | ||
| -- expected type parameter | ||
... | ||
LL | let foo2 = Foo { t: t2, ..d }; | ||
| ^ expected type parameter `T2`, found type parameter `T` | ||
| | ||
= note: expected type parameter `T2` | ||
found type parameter `T` | ||
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound | ||
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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.
Why are you throwing this result away?
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.
The purpose of this code is to instantiate the uninstantiated
infer
generic created for base_expr incheck_expr_struct_fields
by calling therelate_generic_arg
function. IfErr
is returned, the generic type has been instantiated by the type ofbase_expr
itself and is not constrained by the created struct (constrained ones hav been checked incheck_expr_struct_fields
).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.
Perhaps we can have a more accurate approach, such as maintaining a non-constrained generic indexes list for
base_expr
.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.
Given that this is purely for diagnostics, this approach might be fine, but including what you wrote above as a comment would be welcome.
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.
I'm confused by this comment. This isn't purely diagnostic, I thought? My understanding is that this function affects type inference -- notably,
relate_generic_arg
is also not an atomic operation, so even if it returnsErr
, it can still partially constrain inference variables inside of that.I'm not convinced that this approach is completely sound. I would be comfortable with something more along the lines of:
At least in that case it's easier to reason about when we're allowed to unify unconstrained inference variables and when we expect to raise errors.