-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[NLL] Check user types are well-formed #54942
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
|
||
use borrow_check::nll::constraints::OutlivesConstraint; | ||
use borrow_check::nll::type_check::{BorrowCheckContext, Locations}; | ||
use rustc::infer::canonical::{Canonical, CanonicalVarInfos}; | ||
use rustc::infer::canonical::{Canonical, CanonicalVarInfos, CanonicalVarValues}; | ||
use rustc::infer::{InferCtxt, NLLRegionVariableOrigin}; | ||
use rustc::mir::ConstraintCategory; | ||
use rustc::traits::query::Fallible; | ||
|
@@ -70,7 +70,7 @@ pub(super) fn relate_type_and_user_type<'tcx>( | |
locations: Locations, | ||
category: ConstraintCategory, | ||
borrowck_context: Option<&mut BorrowCheckContext<'_, 'tcx>>, | ||
) -> Fallible<()> { | ||
) -> Fallible<Ty<'tcx>> { | ||
debug!( | ||
"sub_type_and_user_type(a={:?}, b={:?}, locations={:?})", | ||
a, b, locations | ||
|
@@ -85,13 +85,24 @@ pub(super) fn relate_type_and_user_type<'tcx>( | |
// variance to get the right relationship. | ||
let v1 = ty::Contravariant.xform(v); | ||
|
||
TypeRelating::new( | ||
let mut type_relating = TypeRelating::new( | ||
infcx.tcx, | ||
NllTypeRelatingDelegate::new(infcx, borrowck_context, locations, category), | ||
v1, | ||
b_variables, | ||
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. b_variables 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. b is destructured just above this. 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. Would you mind pointing me to the line please ? 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. line 79 |
||
).relate(&b_value, &a)?; | ||
Ok(()) | ||
); | ||
type_relating.relate(&b_value, &a)?; | ||
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. type_relating.relate(&b_value, &a)?; Can you explain to me what this line does ? 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. It checks that So if 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. @matthewjasper Thanks a lot for the explanation. Really appreciate it. |
||
|
||
Ok(b.substitute( | ||
infcx.tcx, | ||
&CanonicalVarValues { | ||
var_values: type_relating | ||
.canonical_var_values | ||
.into_iter() | ||
.map(|x| x.expect("unsubstituted canonical variable")) | ||
.collect(), | ||
}, | ||
)) | ||
} | ||
|
||
struct TypeRelating<'me, 'gcx: 'tcx, 'tcx: 'me, D> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
error: unsatisfied lifetime constraints | ||
--> $DIR/associated-types-subtyping-1.rs:36:13 | ||
| | ||
LL | fn method2<'a,'b,T>(x: &'a T, y: &'b T) | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
... | ||
LL | let _c: <T as Trait<'b>>::Type = a; //~ ERROR E0623 | ||
| ^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'b` must outlive `'a` | ||
|
||
error: unsatisfied lifetime constraints | ||
--> $DIR/associated-types-subtyping-1.rs:44:12 | ||
| | ||
LL | fn method3<'a,'b,T>(x: &'a T, y: &'b T) | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
... | ||
LL | let b: <T as Trait<'b>>::Type = make_any(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'b` must outlive `'a` | ||
|
||
error: aborting due to 2 previous errors | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
error: unsatisfied lifetime constraints | ||
--> $DIR/regions-assoc-type-in-supertrait-outlives-container.rs:52:13 | ||
| | ||
LL | fn with_assoc<'a,'b>() { | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
... | ||
LL | let _x: &'a WithAssoc<TheType<'b>> = loop { }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'b` must outlive `'a` | ||
|
||
error: aborting due to previous error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,6 @@ | |
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// ignore-compare-mode-nll | ||
|
||
// Test that we are imposing the requirement that every associated | ||
// type of a bound that appears in the where clause on a struct must | ||
// outlive the location in which the type appears, even when the | ||
|
@@ -49,7 +47,10 @@ fn with_assoc<'a,'b>() { | |
// outlive 'a. In this case, that means TheType<'b>::TheAssocType, | ||
// which is &'b (), must outlive 'a. | ||
|
||
let _: &'a WithAssoc<TheType<'b>> = loop { }; //~ ERROR reference has a longer lifetime | ||
// FIXME (#54943) NLL doesn't enforce WF condition in unreachable code if | ||
// `_x` is changed to `_` | ||
let _x: &'a WithAssoc<TheType<'b>> = loop { }; | ||
//~^ ERROR reference has a longer lifetime | ||
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. Can we ope an issue regarding enforcing WF conditions in dead code and put a FIXME here like this: FIXME(#XXX) -- NLL doesn't enforce WF conditions (or any type annotations) in dead code. |
||
} | ||
|
||
fn main() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
error: unsatisfied lifetime constraints | ||
--> $DIR/regions-free-region-ordering-caller.rs:18:12 | ||
| | ||
LL | fn call2<'a, 'b>(a: &'a usize, b: &'b usize) { | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
LL | let z: Option<&'b &'a usize> = None;//~ ERROR E0623 | ||
| ^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'b` | ||
|
||
error: unsatisfied lifetime constraints | ||
--> $DIR/regions-free-region-ordering-caller.rs:23:12 | ||
| | ||
LL | fn call3<'a, 'b>(a: &'a usize, b: &'b usize) { | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
LL | let y: Paramd<'a> = Paramd { x: a }; | ||
LL | let z: Option<&'b Paramd<'a>> = None;//~ ERROR E0623 | ||
| ^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'b` | ||
|
||
error: unsatisfied lifetime constraints | ||
--> $DIR/regions-free-region-ordering-caller.rs:27:12 | ||
| | ||
LL | fn call4<'a, 'b>(a: &'a usize, b: &'b usize) { | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
LL | let z: Option<&'a &'b usize> = None;//~ ERROR E0623 | ||
| ^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'b` must outlive `'a` | ||
|
||
error: aborting due to 3 previous errors | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error[E0309]: the parameter type `T` may not live long enough | ||
--> $DIR/regions-implied-bounds-projection-gap-1.rs:26:5 | ||
| | ||
LL | wf::<&'x T>(); | ||
| ^^^^^^^^^^^^^ | ||
| | ||
= help: consider adding an explicit lifetime bound `T: 'x`... | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0309`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
error: unsatisfied lifetime constraints | ||
--> $DIR/regions-outlives-projection-container-wc.rs:46:13 | ||
| | ||
LL | fn with_assoc<'a,'b>() { | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
... | ||
LL | let _x: &'a WithAssoc<TheType<'b>> = loop { }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'b` must outlive `'a` | ||
|
||
error: aborting due to previous error | ||
|
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.
It is okay to have a variable name the same as the module name ?
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.
yes