Skip to content

Commit

Permalink
Taint infcx when reporting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jun 19, 2024
1 parent a183989 commit 3594a19
Show file tree
Hide file tree
Showing 36 changed files with 470 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
for (error, suppressed) in iter::zip(&errors, &is_suppressed) {
if !suppressed && error.obligation.cause.span.from_expansion() == from_expansion {
let guar = self.report_fulfillment_error(error);
self.infcx.set_tainted_by_errors(guar);
reported = Some(guar);
// We want to ignore desugarings here: spans are equivalent even
// if one is the result of a desugaring and the other is not.
Expand Down Expand Up @@ -2686,22 +2687,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}

// Given some `ConstArgHasType(?x, usize)`, we should not emit an error such as
// "type annotations needed: cannot satisfy the constant `_` has type `usize`"
// Instead we should emit a normal error suggesting the user to turbofish the
// const parameter that is currently being inferred. Unfortunately we cannot
// nicely emit such an error so we delay an ICE incase nobody else reports it
// for us.
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
return self.tcx.sess.dcx().span_delayed_bug(
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ..)) => self
.emit_inference_failure_err(
obligation.cause.body_id,
span,
format!(
"`ambiguous ConstArgHasType({:?}, {:?}) unaccompanied by inference error`",
ct, ty
),
);
}

ct.into(),
ErrorCode::E0284,
true,
),
ty::PredicateKind::NormalizesTo(ty::NormalizesTo { alias, term })
if term.is_infer() =>
{
Expand Down
38 changes: 0 additions & 38 deletions tests/crashes/122044.rs

This file was deleted.

13 changes: 0 additions & 13 deletions tests/crashes/123255.rs

This file was deleted.

2 changes: 2 additions & 0 deletions tests/incremental/const-generics/issue-64087.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ fn combinator<T, const S: usize>() -> [T; S] {}
fn main() {
combinator().into_iter();
//[cfail1]~^ ERROR type annotations needed
//[cfail1]~| ERROR type annotations needed
//[cfail1]~| ERROR type annotations needed
}
1 change: 1 addition & 0 deletions tests/ui/const-generics/defaults/doesnt_infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ fn main() {
let foo = Foo::<1>::foo();
let foo = Foo::foo();
//~^ ERROR type annotations needed for `Foo<_>`
//~| ERROR type annotations needed
}
31 changes: 27 additions & 4 deletions tests/ui/const-generics/defaults/doesnt_infer.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
error[E0282]: type annotations needed for `Foo<_>`
error[E0284]: type annotations needed for `Foo<_>`
--> $DIR/doesnt_infer.rs:13:9
|
LL | let foo = Foo::foo();
| ^^^
| ^^^ ---------- type must be known at this point
|
note: required by a bound in `Foo::<N>::foo`
--> $DIR/doesnt_infer.rs:5:6
|
LL | impl<const N: u32> Foo<N> {
| ^^^^^^^^^^^^ required by this bound in `Foo::<N>::foo`
LL | fn foo() -> Self {
| --- required by a bound in this associated function
help: consider giving `foo` an explicit type, where the value of const parameter `N` is specified
|
LL | let foo: Foo<N> = Foo::foo();
| ++++++++

error[E0284]: type annotations needed for `Foo<_>`
--> $DIR/doesnt_infer.rs:13:9
|
LL | let foo = Foo::foo();
| ^^^ --- type must be known at this point
|
note: required by a bound in `Foo`
--> $DIR/doesnt_infer.rs:3:12
|
LL | struct Foo<const N: u32 = 2>;
| ^^^^^^^^^^^^^^^^ required by this bound in `Foo`
help: consider giving `foo` an explicit type, where the value of const parameter `N` is specified
|
LL | let foo: Foo<N> = Foo::foo();
| ++++++++

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

For more information about this error, try `rustc --explain E0282`.
For more information about this error, try `rustc --explain E0284`.
9 changes: 7 additions & 2 deletions tests/ui/const-generics/defaults/rp_impl_trait_fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@ LL | 1_u64
|
= help: the trait `Traitor<1, 2>` is implemented for `u64`

error[E0282]: type annotations needed
error[E0284]: type annotations needed
--> $DIR/rp_impl_trait_fail.rs:28:5
|
LL | uwu();
| ^^^ cannot infer the value of the const parameter `N` declared on the function `uwu`
|
note: required by a bound in `uwu`
--> $DIR/rp_impl_trait_fail.rs:16:8
|
LL | fn uwu<const N: u8>() -> impl Traitor<N> {
| ^^^^^^^^^^^ required by this bound in `uwu`
help: consider specifying the generic argument
|
LL | uwu::<N>();
| +++++

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0277, E0282.
Some errors have detailed explanations: E0277, E0284.
For more information about an error, try `rustc --explain E0277`.
1 change: 1 addition & 0 deletions tests/ui/const-generics/generic_arg_infer/issue-91614.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ use std::simd::Mask;
fn main() {
let y = Mask::<_, _>::splat(false);
//~^ ERROR: type annotations needed
//~| ERROR type annotations needed
}
28 changes: 17 additions & 11 deletions tests/ui/const-generics/generic_arg_infer/issue-91614.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
error[E0283]: type annotations needed for `Mask<_, _>`
error[E0284]: type annotations needed for `Mask<_, _>`
--> $DIR/issue-91614.rs:6:9
|
LL | let y = Mask::<_, _>::splat(false);
| ^ -------------------------- type must be known at this point
|
= note: cannot satisfy `_: MaskElement`
= help: the following types implement trait `MaskElement`:
i16
i32
i64
i8
isize
note: required by a bound in `Mask::<T, N>::splat`
--> $SRC_DIR/core/src/../../portable-simd/crates/core_simd/src/masks.rs:LL:COL
help: consider giving `y` an explicit type, where the type for type parameter `T` is specified
help: consider giving `y` an explicit type, where the value of const parameter `N` is specified
|
LL | let y: Mask<T, N> = Mask::<_, _>::splat(false);
| ++++++++++++

error: aborting due to 1 previous error
error[E0284]: type annotations needed for `Mask<_, _>`
--> $DIR/issue-91614.rs:6:9
|
LL | let y = Mask::<_, _>::splat(false);
| ^ ------------ type must be known at this point
|
note: required by a bound in `Mask`
--> $SRC_DIR/core/src/../../portable-simd/crates/core_simd/src/masks.rs:LL:COL
help: consider giving `y` an explicit type, where the value of const parameter `N` is specified
|
LL | let y: Mask<T, N> = Mask::<_, _>::splat(false);
| ++++++++++++

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0283`.
For more information about this error, try `rustc --explain E0284`.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,41 @@ help: try adding a `where` bound
LL | pub const fn new() -> Self where [(); Self::SIZE]: {
| +++++++++++++++++++++++

error[E0282]: type annotations needed for `ArrayHolder<_>`
error[E0284]: type annotations needed for `ArrayHolder<_>`
--> $DIR/issue-62504.rs:26:9
|
LL | let mut array = ArrayHolder::new();
| ^^^^^^^^^
| ^^^^^^^^^ ------------------ type must be known at this point
|
note: required by a bound in `ArrayHolder::<X>::new`
--> $DIR/issue-62504.rs:16:6
|
LL | impl<const X: usize> ArrayHolder<X> {
| ^^^^^^^^^^^^^^ required by this bound in `ArrayHolder::<X>::new`
LL | pub const fn new() -> Self {
| --- required by a bound in this associated function
help: consider giving `array` an explicit type, where the value of const parameter `X` is specified
|
LL | let mut array: ArrayHolder<X> = ArrayHolder::new();
| ++++++++++++++++

error[E0284]: type annotations needed for `ArrayHolder<_>`
--> $DIR/issue-62504.rs:26:9
|
LL | let mut array = ArrayHolder::new();
| ^^^^^^^^^ ----------- type must be known at this point
|
note: required by a bound in `ArrayHolder`
--> $DIR/issue-62504.rs:14:20
|
LL | struct ArrayHolder<const X: usize>([u32; X]);
| ^^^^^^^^^^^^^^ required by this bound in `ArrayHolder`
help: consider giving `array` an explicit type, where the value of const parameter `X` is specified
|
LL | let mut array: ArrayHolder<X> = ArrayHolder::new();
| ++++++++++++++++

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

Some errors have detailed explanations: E0282, E0308.
For more information about an error, try `rustc --explain E0282`.
Some errors have detailed explanations: E0284, E0308.
For more information about an error, try `rustc --explain E0284`.
33 changes: 28 additions & 5 deletions tests/ui/const-generics/generic_const_exprs/issue-62504.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,41 @@ note: tuple struct defined here
LL | struct ArrayHolder<const X: usize>([u32; X]);
| ^^^^^^^^^^^

error[E0282]: type annotations needed for `ArrayHolder<_>`
error[E0284]: type annotations needed for `ArrayHolder<_>`
--> $DIR/issue-62504.rs:26:9
|
LL | let mut array = ArrayHolder::new();
| ^^^^^^^^^
| ^^^^^^^^^ ------------------ type must be known at this point
|
note: required by a bound in `ArrayHolder::<X>::new`
--> $DIR/issue-62504.rs:16:6
|
LL | impl<const X: usize> ArrayHolder<X> {
| ^^^^^^^^^^^^^^ required by this bound in `ArrayHolder::<X>::new`
LL | pub const fn new() -> Self {
| --- required by a bound in this associated function
help: consider giving `array` an explicit type, where the value of const parameter `X` is specified
|
LL | let mut array: ArrayHolder<X> = ArrayHolder::new();
| ++++++++++++++++

error[E0284]: type annotations needed for `ArrayHolder<_>`
--> $DIR/issue-62504.rs:26:9
|
LL | let mut array = ArrayHolder::new();
| ^^^^^^^^^ ----------- type must be known at this point
|
note: required by a bound in `ArrayHolder`
--> $DIR/issue-62504.rs:14:20
|
LL | struct ArrayHolder<const X: usize>([u32; X]);
| ^^^^^^^^^^^^^^ required by this bound in `ArrayHolder`
help: consider giving `array` an explicit type, where the value of const parameter `X` is specified
|
LL | let mut array: ArrayHolder<X> = ArrayHolder::new();
| ++++++++++++++++

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

Some errors have detailed explanations: E0282, E0308.
For more information about an error, try `rustc --explain E0282`.
Some errors have detailed explanations: E0284, E0308.
For more information about an error, try `rustc --explain E0284`.
1 change: 1 addition & 0 deletions tests/ui/const-generics/generic_const_exprs/issue-62504.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ impl<const X: usize> ArrayHolder<X> {
fn main() {
let mut array = ArrayHolder::new();
//~^ ERROR: type annotations needed
//~| ERROR type annotations needed
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ fn use_dyn<const N: usize>(v: &dyn Foo<N>) where [u8; N + 1]: Sized {
fn main() {
use_dyn(&());
//~^ ERROR type annotations needed
//~| ERROR type annotations needed
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,36 @@ LL | use_dyn(&());
| ^^^^^^^ cannot infer the value of the const parameter `N` declared on the function `use_dyn`
|
note: required by a bound in `use_dyn`
--> $DIR/object-safety-ok-infer-err.rs:14:55
--> $DIR/object-safety-ok-infer-err.rs:14:12
|
LL | fn use_dyn<const N: usize>(v: &dyn Foo<N>) where [u8; N + 1]: Sized {
| ^^^^^ required by this bound in `use_dyn`
| ^^^^^^^^^^^^^^ required by this bound in `use_dyn`
help: consider specifying the generic argument
|
LL | use_dyn::<N>(&());
| +++++

error: aborting due to 1 previous error
error[E0284]: type annotations needed
--> $DIR/object-safety-ok-infer-err.rs:19:5
|
LL | use_dyn(&());
| ^^^^^^^ --- type must be known at this point
| |
| cannot infer the value of the const parameter `N` declared on the function `use_dyn`
|
note: required for `()` to implement `Foo<_>`
--> $DIR/object-safety-ok-infer-err.rs:8:22
|
LL | impl<const N: usize> Foo<N> for () {
| -------------- ^^^^^^ ^^
| |
| unsatisfied trait bound introduced here
= note: required for the cast from `&()` to `&dyn Foo<_>`
help: consider specifying the generic argument
|
LL | use_dyn::<N>(&());
| +++++

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0284`.
9 changes: 7 additions & 2 deletions tests/ui/const-generics/infer/cannot-infer-const-args.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
error[E0282]: type annotations needed
error[E0284]: type annotations needed
--> $DIR/cannot-infer-const-args.rs:6:5
|
LL | foo();
| ^^^ cannot infer the value of the const parameter `X` declared on the function `foo`
|
note: required by a bound in `foo`
--> $DIR/cannot-infer-const-args.rs:1:8
|
LL | fn foo<const X: usize>() -> usize {
| ^^^^^^^^^^^^^^ required by this bound in `foo`
help: consider specifying the generic argument
|
LL | foo::<X>();
| +++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0282`.
For more information about this error, try `rustc --explain E0284`.
Loading

0 comments on commit 3594a19

Please sign in to comment.