Skip to content

Commit

Permalink
Taint obligations in confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Dec 3, 2024
1 parent 1dd3d2a commit 3377c2f
Show file tree
Hide file tree
Showing 25 changed files with 97 additions and 212 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
let is_method = mode == Mode::MethodCall;
let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;
if let Err(guar) = unsatisfied_predicates.error_reported() {
return guar;
}

let similar_candidate = no_match_data.similar_candidate;
let item_kind = if is_method {
"method"
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_middle/src/ty/generic_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rustc_errors::{DiagArgValue, IntoDiagArg};
use rustc_hir::def_id::DefId;
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable, extension};
use rustc_serialize::{Decodable, Encodable};
use rustc_span::ErrorGuaranteed;
use rustc_type_ir::WithCachedTypeInfo;
use smallvec::SmallVec;

Expand Down Expand Up @@ -300,6 +301,14 @@ impl<'tcx> GenericArg<'tcx> {
GenericArgKind::Const(ct) => ct.is_ct_infer(),
}
}

pub fn to_error(self, tcx: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Self {
match self.unpack() {
ty::GenericArgKind::Lifetime(_) => ty::Region::new_error(tcx, guar).into(),
ty::GenericArgKind::Type(_) => Ty::new_error(tcx, guar).into(),
ty::GenericArgKind::Const(_) => ty::Const::new_error(tcx, guar).into(),
}
}
}

impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for GenericArg<'a> {
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2828,6 +2828,18 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
// `$1: Copy`, so we must ensure the obligations are emitted in
// that order.
let predicates = tcx.predicates_of(def_id);
if let Err(guar) = predicates.errored_due_to_unconstrained_params {
self.infcx.set_tainted_by_errors(guar);
// Constrain any inference variables to their error variant to ensure unconstrained
// generic parameters don't leak as they'll never get constrained.
for arg in args {
let _ = self.infcx.at(cause, param_env).eq(
DefineOpaqueTypes::Yes,
arg,
arg.to_error(tcx, guar),
);
}
}
assert_eq!(predicates.parent, None);
let predicates = predicates.instantiate_own(tcx, args);
let mut obligations = PredicateObligations::with_capacity(predicates.len());
Expand Down
23 changes: 0 additions & 23 deletions tests/crashes/123141.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/crashes/125874.rs

This file was deleted.

11 changes: 0 additions & 11 deletions tests/crashes/126942.rs

This file was deleted.

18 changes: 18 additions & 0 deletions tests/ui/associated-item/unconstrained_impl_param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! This test used to ICE during the normalization of
//! `I`'s type, because of the mismatch of generic parameters
//! on the impl with the generic parameters the projection can
//! fulfill.
struct Thing;

pub trait Every {
type Assoc;
}
impl<T: ?Sized> Every for Thing {
//~^ ERROR: `T` is not constrained
type Assoc = T;
}

static I: <Thing as Every>::Assoc = 3;

fn main() {}
9 changes: 9 additions & 0 deletions tests/ui/associated-item/unconstrained_impl_param.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
--> $DIR/unconstrained_impl_param.rs:11:6
|
LL | impl<T: ?Sized> Every for Thing {
| ^ unconstrained type parameter

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0207`.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ where
{
fn unimplemented(self, _: &Foo) -> Self::Output {
//~^ ERROR method `unimplemented` is not a member of trait `std::ops::Add`
//~| ERROR type annotations needed
loop {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ error[E0407]: method `unimplemented` is not a member of trait `std::ops::Add`
|
LL | / fn unimplemented(self, _: &Foo) -> Self::Output {
LL | |
LL | |
LL | | loop {}
LL | | }
| |_____^ not a member of trait `std::ops::Add`
Expand All @@ -26,21 +25,7 @@ LL | impl<'a, const NUM: usize> std::ops::Add<&'a Foo> for Foo
= note: expressions using a const parameter must map each value to a distinct output value
= note: proving the result of expressions other than the parameter are unique is not supported

error[E0284]: type annotations needed
--> $DIR/post-analysis-user-facing-param-env.rs:11:40
|
LL | fn unimplemented(self, _: &Foo) -> Self::Output {
| ^^^^^^^^^^^^ cannot infer the value of const parameter `NUM`
|
note: required for `Foo` to implement `Add<&'a Foo>`
--> $DIR/post-analysis-user-facing-param-env.rs:6:28
|
LL | impl<'a, const NUM: usize> std::ops::Add<&'a Foo> for Foo
| ---------------- ^^^^^^^^^^^^^^^^^^^^^^ ^^^
| |
| unsatisfied trait bound introduced here

error: aborting due to 3 previous errors; 1 warning emitted
error: aborting due to 2 previous errors; 1 warning emitted

Some errors have detailed explanations: E0207, E0284, E0407.
Some errors have detailed explanations: E0207, E0407.
For more information about an error, try `rustc --explain E0207`.
1 change: 1 addition & 0 deletions tests/ui/const-generics/kind_mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ pub fn remove_key<K, S: SubsetExcept<K>>() -> S {

fn main() {
let map: KeyHolder<0> = remove_key::<_, _>();
//~^ ERROR: type annotations needed
}
11 changes: 9 additions & 2 deletions tests/ui/const-generics/kind_mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ LL | impl<K> ContainsKey<K> for KeyHolder<K> {}
| |
| help: consider changing this type parameter to a const parameter: `const K: u8`

error: aborting due to 2 previous errors
error[E0282]: type annotations needed
--> $DIR/kind_mismatch.rs:22:29
|
LL | let map: KeyHolder<0> = remove_key::<_, _>();
| ^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `K` declared on the function `remove_key`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0747`.
Some errors have detailed explanations: E0282, E0747.
For more information about an error, try `rustc --explain E0282`.
14 changes: 14 additions & 0 deletions tests/ui/const-generics/kind_mismatch2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
struct Node<const D: usize> {}

impl<const D: usize> Node<{ D }>
where
SmallVec<D>:, //~ ERROR: constant provided when a type was expected
{
fn new() {}
}

struct SmallVec<T>(T);

fn main() {
Node::new();
}
9 changes: 9 additions & 0 deletions tests/ui/const-generics/kind_mismatch2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0747]: constant provided when a type was expected
--> $DIR/kind_mismatch2.rs:5:14
|
LL | SmallVec<D>:,
| ^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0747`.
68 changes: 1 addition & 67 deletions tests/ui/generic-associated-types/bugs/issue-87735.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,73 +22,7 @@ help: consider adding an explicit lifetime bound
LL | type Output<'a> = FooRef<'a, U> where Self: 'a, U: 'a;
| +++++++

error[E0309]: the parameter type `T` may not live long enough
--> $DIR/issue-87735.rs:31:15
|
LL | impl<'b, T, U> AsRef2 for Foo<T>
| -- the parameter type `T` must be valid for the lifetime `'b` as defined here...
...
LL | T: AsRef2<Output<'b> = &'b [U]>,
| ^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
|
note: ...that is required by this bound
--> $DIR/issue-87735.rs:7:31
|
LL | type Output<'a> where Self: 'a;
| ^^
help: consider adding an explicit lifetime bound
|
LL | T: AsRef2<Output<'b> = &'b [U]> + 'b,
| ++++

error[E0309]: the parameter type `T` may not live long enough
--> $DIR/issue-87735.rs:36:31
|
LL | impl<'b, T, U> AsRef2 for Foo<T>
| -- the parameter type `T` must be valid for the lifetime `'b` as defined here...
...
LL | fn as_ref2<'a>(&'a self) -> Self::Output<'a> {
| ^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
|
note: ...that is required by this bound
--> $DIR/issue-87735.rs:7:31
|
LL | type Output<'a> where Self: 'a;
| ^^
help: consider adding an explicit lifetime bound
|
LL | T: AsRef2<Output<'b> = &'b [U]> + 'b,
| ++++

error: lifetime may not live long enough
--> $DIR/issue-87735.rs:37:5
|
LL | impl<'b, T, U> AsRef2 for Foo<T>
| -- lifetime `'b` defined here
...
LL | fn as_ref2<'a>(&'a self) -> Self::Output<'a> {
| -- lifetime `'a` defined here
LL | FooRef(self.0.as_ref2())
| ^^^^^^^^^^^^^^^^^^^^^^^^ method was supposed to return data with lifetime `'a` but it is returning data with lifetime `'b`
|
= help: consider adding the following bound: `'b: 'a`

error: lifetime may not live long enough
--> $DIR/issue-87735.rs:37:12
|
LL | impl<'b, T, U> AsRef2 for Foo<T>
| -- lifetime `'b` defined here
...
LL | fn as_ref2<'a>(&'a self) -> Self::Output<'a> {
| -- lifetime `'a` defined here
LL | FooRef(self.0.as_ref2())
| ^^^^^^^^^^^^^^^^ argument requires that `'a` must outlive `'b`
|
= help: consider adding the following bound: `'a: 'b`

help: `'b` and `'a` must be the same: replace one with the other

error: aborting due to 6 previous errors
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0207, E0309.
For more information about an error, try `rustc --explain E0207`.
1 change: 0 additions & 1 deletion tests/ui/impl-trait/in-trait/refine-resolution-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ impl<T: ?Sized> Mirror for () {

pub trait First {
async fn first() -> <() as Mirror>::Assoc;
//~^ ERROR type annotations needed
}

impl First for () {
Expand Down
11 changes: 2 additions & 9 deletions tests/ui/impl-trait/in-trait/refine-resolution-errors.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
LL | impl<T: ?Sized> Mirror for () {
| ^ unconstrained type parameter

error[E0282]: type annotations needed
--> $DIR/refine-resolution-errors.rs:15:5
|
LL | async fn first() -> <() as Mirror>::Assoc;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0207, E0282.
For more information about an error, try `rustc --explain E0207`.
For more information about this error, try `rustc --explain E0207`.
2 changes: 0 additions & 2 deletions tests/ui/impl-trait/issues/issue-87340.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ impl<T> X for () {
//~^ ERROR `T` is not constrained by the impl trait, self type, or predicates
type I = impl Sized;
fn f() -> Self::I {}
//~^ ERROR type annotations needed
//~| ERROR type annotations needed
}

fn main() {}
17 changes: 2 additions & 15 deletions tests/ui/impl-trait/issues/issue-87340.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@ error[E0207]: the type parameter `T` is not constrained by the impl trait, self
LL | impl<T> X for () {
| ^ unconstrained type parameter

error[E0282]: type annotations needed
--> $DIR/issue-87340.rs:11:23
|
LL | fn f() -> Self::I {}
| ^^ cannot infer type for type parameter `T`

error[E0282]: type annotations needed
--> $DIR/issue-87340.rs:11:15
|
LL | fn f() -> Self::I {}
| ^^^^^^^ cannot infer type for type parameter `T`

error: aborting due to 3 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0207, E0282.
For more information about an error, try `rustc --explain E0207`.
For more information about this error, try `rustc --explain E0207`.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ impl<T: MyFrom<Phantom2<DummyT<U>>>, U> MyIndex<DummyT<T>> for Scope<U> {
//~^ ERROR the type parameter `T` is not constrained by the impl
type O = T;
fn my_index(self) -> Self::O {
//~^ ERROR item does not constrain
MyFrom::my_from(self.0).ok().unwrap()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ note: this opaque type is in the signature
LL | type DummyT<T> = impl F;
| ^^^^^^

error: item does not constrain `DummyT::{opaque#0}`, but has it in its signature
--> $DIR/ice-failed-to-resolve-instance-for-110696.rs:44:8
|
LL | fn my_index(self) -> Self::O {
| ^^^^^^^^
|
= note: consider moving the opaque type's declaration and defining uses into a separate module
note: this opaque type is in the signature
--> $DIR/ice-failed-to-resolve-instance-for-110696.rs:20:18
|
LL | type DummyT<T> = impl F;
| ^^^^^^

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0207`.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ impl<T> X for () {
//~^ ERROR the type parameter `T` is not constrained
type I = impl Sized;
fn f() -> Self::I {}
//~^ ERROR type annotations needed
//~| ERROR type annotations needed
}

fn main() {}
Loading

0 comments on commit 3377c2f

Please sign in to comment.