Skip to content
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

[WIP] reject more impossible trivial bounds (HRTBs and trivial after normalization) #95611

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 50 additions & 18 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use rustc_infer::infer::outlives::obligations::TypeOutlives;
use rustc_infer::infer::region_constraints::GenericKind;
use rustc_infer::infer::{self, RegionckMode};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::TraitEngine;
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts, Subst};
use rustc_middle::ty::trait_def::TraitSpecializationKind;
Expand Down Expand Up @@ -1796,7 +1797,7 @@ fn report_bivariance(

/// Feature gates RFC 2056 -- trivial bounds, checking for global bounds that
/// aren't true.
fn check_false_global_bounds(fcx: &FnCtxt<'_, '_>, mut span: Span, id: hir::HirId) {
fn check_false_global_bounds(fcx: &FnCtxt<'_, '_>, span: Span, id: hir::HirId) {
let empty_env = ty::ParamEnv::empty();

let def_id = fcx.tcx.hir().local_def_id(id);
Expand All @@ -1807,33 +1808,64 @@ fn check_false_global_bounds(fcx: &FnCtxt<'_, '_>, mut span: Span, id: hir::HirI

for obligation in implied_obligations {
let pred = obligation.predicate;

// only use the span of the predicate clause (#90869)
let hir_node = fcx.tcx.hir().find(id);
let span = if let Some(hir::Generics { where_clause, .. }) =
hir_node.and_then(|node| node.generics())
{
let obligation_span = obligation.cause.span(fcx.tcx);
where_clause
.predicates
.iter()
// There seems to be no better way to find out which predicate we are in
.find(|pred| pred.span().contains(obligation_span))
.map(|pred| pred.span())
.unwrap_or(obligation_span)
} else {
span
};

// Match the existing behavior.
if pred.is_global() && !pred.has_late_bound_regions() {
let pred = fcx.normalize_associated_types_in(span, pred);
let hir_node = fcx.tcx.hir().find(id);

// only use the span of the predicate clause (#90869)

if let Some(hir::Generics { where_clause, .. }) =
hir_node.and_then(|node| node.generics())
{
let obligation_span = obligation.cause.span(fcx.tcx);

span = where_clause
.predicates
.iter()
// There seems to be no better way to find out which predicate we are in
.find(|pred| pred.span().contains(obligation_span))
.map(|pred| pred.span())
.unwrap_or(obligation_span);
}

let obligation = traits::Obligation::new(
traits::ObligationCause::new(span, id, traits::TrivialBound),
empty_env,
pred,
);
fcx.register_predicate(obligation);
} else {
let infer::InferOk { value: norm_pred, obligations } =
fcx.normalize_associated_types_in_as_infer_ok(span, pred);
if norm_pred.is_global() {
fcx.probe(|_| {
let mut fulfillment_cx = traits::FulfillmentContext::new_in_snapshot();
for obligation in obligations {
fulfillment_cx.register_predicate_obligation(&fcx, obligation);
}
fulfillment_cx.register_predicate_obligation(&fcx, traits::Obligation::new(
traits::ObligationCause::new(span, id, traits::TrivialBound),
empty_env,
norm_pred,
));
if !fulfillment_cx.select_all_or_error(&fcx).is_empty() {
let mut diag = fcx
.tcx
.sess
// NOTE: Error for the crater run
.struct_span_warn(span, "where-clause bound is impossible to satisfy");
diag.note(format!("the bound `{}` was previously accepted, but it may become a hard error in a future release", pred));
if fcx.sess().opts.unstable_features.is_nightly_build() {
diag.help(
"add `#![feature(trivial_bounds)]` to the crate attributes to allow it",
);
}
diag.emit();
}
})
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: fatal error triggered by #[rustc_error]
--> $DIR/bound-lifetime-in-binding-only.rs:71:1
--> $DIR/bound-lifetime-in-binding-only.rs:75:1
|
LL | fn main() { }
| ^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ fn elision<T: Fn() -> &i32>() {

struct Parameterized<'a> { x: &'a str }

impl<'a> Foo for Parameterized<'a> {
type Item = &'a i32;
}

#[cfg(ok)]
fn ok1<T: for<'a> Fn(&Parameterized<'a>) -> &'a i32>() {
}
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/associated-types/issue-69398.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub trait Broken {
impl<T> Broken for T {
type Assoc = ();
fn broken(&self) where Self::Assoc: Foo {
//~^ WARN where-clause bound is impossible to satisfy
let _x: <Self::Assoc as Foo>::Bar;
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/associated-types/issue-69398.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: where-clause bound is impossible to satisfy
--> $DIR/issue-69398.rs:14:28
|
LL | fn broken(&self) where Self::Assoc: Foo {
| ^^^^^^^^^^^^^^^^
|
= note: the bound `<T as Broken>::Assoc: Foo` was previously accepted, but it may become a hard error in a future release
= help: add `#![feature(trivial_bounds)]` to the crate attributes to allow it

warning: 1 warning emitted

11 changes: 9 additions & 2 deletions src/test/ui/consts/issue-67696-const-prop-ice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ trait A {
}

impl A for [fn(&())] {
fn foo(&self) -> Self where Self: Copy { *(&[] as &[_]) }
fn foo(&self) -> Self where Self: Copy {
//~^ WARN where-clause bound is impossible to satisfy
//~| WARN where-clause bound is impossible to satisfy
//~| WARN where-clause bound is impossible to satisfy
*(&[] as &[_])
}
}

impl A for i32 {
fn foo(&self) -> Self { 3 }
fn foo(&self) -> Self {
3
}
}

fn main() {}
29 changes: 29 additions & 0 deletions src/test/ui/consts/issue-67696-const-prop-ice.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
warning: where-clause bound is impossible to satisfy
--> $DIR/issue-67696-const-prop-ice.rs:13:33
|
LL | fn foo(&self) -> Self where Self: Copy {
| ^^^^^^^^^^
|
= note: the bound `[for<'r> fn(&'r ())]: Copy` was previously accepted, but it may become a hard error in a future release
= help: add `#![feature(trivial_bounds)]` to the crate attributes to allow it

warning: where-clause bound is impossible to satisfy
--> $DIR/issue-67696-const-prop-ice.rs:13:33
|
LL | fn foo(&self) -> Self where Self: Copy {
| ^^^^^^^^^^
|
= note: the bound `[for<'r> fn(&'r ())]: Clone` was previously accepted, but it may become a hard error in a future release
= help: add `#![feature(trivial_bounds)]` to the crate attributes to allow it

warning: where-clause bound is impossible to satisfy
--> $DIR/issue-67696-const-prop-ice.rs:13:33
|
LL | fn foo(&self) -> Self where Self: Copy {
| ^^^^^^^^^^
|
= note: the bound `[for<'r> fn(&'r ())]: Sized` was previously accepted, but it may become a hard error in a future release
= help: add `#![feature(trivial_bounds)]` to the crate attributes to allow it

warning: 3 warnings emitted

2 changes: 2 additions & 0 deletions src/test/ui/feature-gates/feature-gate-trivial_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ fn return_str() -> str where str: Sized { //~ ERROR

// This is currently accepted because the function pointer isn't
// considered global.
// FIXME(compiler-errors): Not for long..
fn global_hr(x: fn(&())) where fn(&()): Foo { // OK
//~^ WARN where-clause bound is impossible to satisfy
x.test();
}

Expand Down
11 changes: 10 additions & 1 deletion src/test/ui/feature-gates/feature-gate-trivial_bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ LL | fn return_str() -> str where str: Sized {
= help: see issue #48214
= help: add `#![feature(trivial_bounds)]` to the crate attributes to enable

error: aborting due to 11 previous errors
warning: where-clause bound is impossible to satisfy
--> $DIR/feature-gate-trivial_bounds.rs:66:32
|
LL | fn global_hr(x: fn(&())) where fn(&()): Foo { // OK
| ^^^^^^^^^^^^
|
= note: the bound `for<'r> fn(&'r ()): Foo` was previously accepted, but it may become a hard error in a future release
= help: add `#![feature(trivial_bounds)]` to the crate attributes to allow it

error: aborting due to 11 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ impl<B: BufferMut, C> BufferUdpStateContext<B> for C {}
trait StackContext
where
Ctx<()>: for<'a> BufferUdpStateContext<&'a ()>,
//~^ WARN where-clause bound is impossible to satisfy
{
type Dispatcher;
}
Expand All @@ -26,6 +27,8 @@ where
struct EthernetWorker<C>(C)
where
Ctx<()>: for<'a> BufferUdpStateContext<&'a ()>;
//~^ WARN where-clause bound is impossible to satisfy

impl<C> EthernetWorker<C> {}
//~^ ERROR: is not satisfied [E0277]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
warning: where-clause bound is impossible to satisfy
--> $DIR/issue-89118.rs:9:5
|
LL | Ctx<()>: for<'a> BufferUdpStateContext<&'a ()>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the bound `for<'a> Ctx<()>: BufferUdpStateContext<&'a ()>` was previously accepted, but it may become a hard error in a future release
= help: add `#![feature(trivial_bounds)]` to the crate attributes to allow it

error[E0277]: the trait bound `for<'a> &'a (): BufferMut` is not satisfied
--> $DIR/issue-89118.rs:19:8
--> $DIR/issue-89118.rs:20:8
|
LL | C: StackContext,
| ^^^^^^^^^^^^ the trait `for<'a> BufferMut` is not implemented for `&'a ()`
Expand All @@ -19,7 +28,7 @@ LL | Ctx<()>: for<'a> BufferUdpStateContext<&'a ()>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `StackContext`

error[E0277]: the trait bound `for<'a> &'a (): BufferMut` is not satisfied
--> $DIR/issue-89118.rs:22:20
--> $DIR/issue-89118.rs:23:20
|
LL | type Handler = Ctx<C::Dispatcher>;
| ^^^^^^^^^^^^^^^^^^ the trait `for<'a> BufferMut` is not implemented for `&'a ()`
Expand All @@ -38,8 +47,17 @@ LL | where
LL | Ctx<()>: for<'a> BufferUdpStateContext<&'a ()>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `StackContext`

warning: where-clause bound is impossible to satisfy
--> $DIR/issue-89118.rs:29:5
|
LL | Ctx<()>: for<'a> BufferUdpStateContext<&'a ()>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the bound `for<'a> Ctx<()>: BufferUdpStateContext<&'a ()>` was previously accepted, but it may become a hard error in a future release
= help: add `#![feature(trivial_bounds)]` to the crate attributes to allow it

error[E0277]: the trait bound `for<'a> &'a (): BufferMut` is not satisfied
--> $DIR/issue-89118.rs:29:9
--> $DIR/issue-89118.rs:32:9
|
LL | impl<C> EthernetWorker<C> {}
| ^^^^^^^^^^^^^^^^^ the trait `for<'a> BufferMut` is not implemented for `&'a ()`
Expand All @@ -50,14 +68,14 @@ note: required because of the requirements on the impl of `for<'a> BufferUdpStat
LL | impl<B: BufferMut, C> BufferUdpStateContext<B> for C {}
| ^^^^^^^^^^^^^^^^^^^^^^^^ ^
note: required by a bound in `EthernetWorker`
--> $DIR/issue-89118.rs:28:14
--> $DIR/issue-89118.rs:29:14
|
LL | struct EthernetWorker<C>(C)
| -------------- required by a bound in this
LL | where
LL | Ctx<()>: for<'a> BufferUdpStateContext<&'a ()>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `EthernetWorker`

error: aborting due to 3 previous errors
error: aborting due to 3 previous errors; 2 warnings emitted

For more information about this error, try `rustc --explain E0277`.
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-36839.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub trait Broken {
impl<T> Broken for T {
type Assoc = ();
fn broken(&self) where Self::Assoc: Foo {
//~^ WARN where-clause bound is impossible to satisfy
let _x: <Self::Assoc as Foo>::Bar;
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/issues/issue-36839.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: where-clause bound is impossible to satisfy
--> $DIR/issue-36839.rs:14:28
|
LL | fn broken(&self) where Self::Assoc: Foo {
| ^^^^^^^^^^^^^^^^
|
= note: the bound `<T as Broken>::Assoc: Foo` was previously accepted, but it may become a hard error in a future release
= help: add `#![feature(trivial_bounds)]` to the crate attributes to allow it

warning: 1 warning emitted

1 change: 1 addition & 0 deletions src/test/ui/issues/issue-39970.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl<'a> Array<'a> for () {
impl Visit for () where
//(): for<'a> Array<'a, Element=&'a ()>, // No ICE
(): for<'a> Array<'a, Element=()>, // ICE
//~^ WARN where-clause bound is impossible to satisfy
{}

fn main() {
Expand Down
13 changes: 11 additions & 2 deletions src/test/ui/issues/issue-39970.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
warning: where-clause bound is impossible to satisfy
--> $DIR/issue-39970.rs:15:5
|
LL | (): for<'a> Array<'a, Element=()>, // ICE
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the bound `for<'a> <() as Array<'a>>::Element == ()` was previously accepted, but it may become a hard error in a future release
= help: add `#![feature(trivial_bounds)]` to the crate attributes to allow it

error[E0271]: type mismatch resolving `for<'a> <() as Array<'a>>::Element == ()`
--> $DIR/issue-39970.rs:19:5
--> $DIR/issue-39970.rs:20:5
|
LL | <() as Visit>::visit();
| ^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `for<'a> <() as Array<'a>>::Element == ()`
Expand All @@ -15,6 +24,6 @@ note: required because of the requirements on the impl of `Visit` for `()`
LL | impl Visit for () where
| ^^^^^ ^^

error: aborting due to previous error
error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0271`.
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-42796.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ impl<T, Smoke> Mirror<Smoke> for T {
}

pub fn poison<S>(victim: String) where <String as Mirror<S>>::Image: Copy {
//~^ WARN where-clause bound is impossible to satisfy
loop { drop(victim); }
}

Expand Down
13 changes: 11 additions & 2 deletions src/test/ui/issues/issue-42796.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
warning: where-clause bound is impossible to satisfy
--> $DIR/issue-42796.rs:9:40
|
LL | pub fn poison<S>(victim: String) where <String as Mirror<S>>::Image: Copy {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the bound `<String as Mirror<S>>::Image: Copy` was previously accepted, but it may become a hard error in a future release
= help: add `#![feature(trivial_bounds)]` to the crate attributes to allow it

error[E0382]: borrow of moved value: `s`
--> $DIR/issue-42796.rs:18:20
--> $DIR/issue-42796.rs:19:20
|
LL | let s = "Hello!".to_owned();
| - move occurs because `s` has type `String`, which does not implement the `Copy` trait
Expand All @@ -11,6 +20,6 @@ LL | println!("{}", s);
|
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error
error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0382`.
1 change: 1 addition & 0 deletions src/test/ui/mir/issue-91745.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub trait Broken {
impl<T> Broken for T {
type Assoc = ();
fn broken(&self) where Self::Assoc: Foo {
//~^ WARN where-clause bound is impossible to satisfy
let _x: <Self::Assoc as Foo>::Bar;
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/mir/issue-91745.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: where-clause bound is impossible to satisfy
--> $DIR/issue-91745.rs:14:28
|
LL | fn broken(&self) where Self::Assoc: Foo {
| ^^^^^^^^^^^^^^^^
|
= note: the bound `<T as Broken>::Assoc: Foo` was previously accepted, but it may become a hard error in a future release
= help: add `#![feature(trivial_bounds)]` to the crate attributes to allow it

warning: 1 warning emitted

Loading