Skip to content

use implied bounds from impl header when comparing trait and impl methods #105548

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

Closed
wants to merge 1 commit into from
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
7 changes: 6 additions & 1 deletion compiler/rustc_hir_analysis/src/check/compare_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,12 @@ fn compare_predicate_entailment<'tcx>(
// Compute placeholder form of impl and trait method tys.
let tcx = infcx.tcx;

let mut wf_tys = FxIndexSet::default();
// Add implied bounds from the impl header.
let mut wf_tys = ocx.assumed_wf_types(
param_env,
impl_m_span,
tcx.local_parent(impl_m.def_id.expect_local()),
);

let impl_sig = infcx.replace_bound_vars_with_fresh_vars(
impl_m_span,
Expand Down
47 changes: 47 additions & 0 deletions src/test/ui/implied-bounds/compare-impl-method-projections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Same as ./compare-impl-method.rs but we shouldn't use implied bounds from projections in impl
// header. See `mod exploit` for why.
// check-fail

trait Project {
type Ty;
}
impl<T> Project for T {
type Ty = ();
}

trait Trait {
fn get();
}

impl<'a, 'b> Trait for <&'a &'b () as Project>::Ty {
fn get()
where
'b: 'a,
//~^ ERROR impl has stricter requirements than trait
{
}
}

mod exploit {
trait Trait<Witness> {
fn extend(self) -> &'static str;
}

impl<'a> Trait<<&'static &'a u8 as super::Project>::Ty> for &'a str {
fn extend(self) -> &'static str
where
'a: 'static,
//~^ ERROR impl has stricter requirements than trait
//~| WARN unnecessary lifetime
{
self
}
}

fn main() {
let val = <&str as Trait<()>>::extend(&String::from("blah blah blah"));
println!("{}", val);
}
}

fn main() {}
29 changes: 29 additions & 0 deletions src/test/ui/implied-bounds/compare-impl-method-projections.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
warning: unnecessary lifetime parameter `'a`
--> $DIR/compare-impl-method-projections.rs:33:13
|
LL | 'a: 'static,
| ^^
|
= help: you can use the `'static` lifetime directly, in place of `'a`

error[E0276]: impl has stricter requirements than trait
--> $DIR/compare-impl-method-projections.rs:19:13
|
LL | fn get();
| --------- definition of `get` from trait
...
LL | 'b: 'a,
| ^^ impl has extra requirement `'b: 'a`

error[E0276]: impl has stricter requirements than trait
--> $DIR/compare-impl-method-projections.rs:33:17
|
LL | fn extend(self) -> &'static str;
| -------------------------------- definition of `extend` from trait
...
LL | 'a: 'static,
| ^^^^^^^ impl has extra requirement `'a: 'static`

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

For more information about this error, try `rustc --explain E0276`.
19 changes: 19 additions & 0 deletions src/test/ui/implied-bounds/compare-impl-method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Check implied bounds are used when comparing trait and impl methods.
// issue: #105495
// check-pass

trait Trait {
fn get();
}

// An implied bound 'b: 'a
impl<'a, 'b> Trait for &'a &'b u8 {
fn get() where 'b: 'a, {}
}

// An explicit bound 'b: 'a
impl<'a, 'b> Trait for (&'a u8, &'b u8) where 'b: 'a, {
fn get() where 'b: 'a, {}
}

fn main() {}