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

implied bounds from impl header are not used in associated functions/consts #98852

Closed
aliemjay opened this issue Jul 3, 2022 · 2 comments · Fixed by #120019
Closed

implied bounds from impl header are not used in associated functions/consts #98852

aliemjay opened this issue Jul 3, 2022 · 2 comments · Fixed by #120019
Labels
A-associated-items Area: Associated items (types, constants & functions) A-implied-bounds Area: Implied bounds / inferred outlives-bounds C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@aliemjay
Copy link
Member

aliemjay commented Jul 3, 2022

The following fails to compile although it shouldn't: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f7287b6897a385360f7190001b58348d

/// A type with an implied bound `'b: 'a`
struct Ty<'a, 'b>(&'a &'b ());

impl<'a, 'b> Ty<'a, 'b> // <- tait or inherent impl
where
    //'b: 'a, // <- fixed by an explicit bound
{
    fn f() { // <- no `Self` in signature
        Self;
        //~^ ERROR lifetime bound not satisfied
        // Cannot prove WF of `Self` because `'b: 'a` is not known to hold
    }
}

Another repro:

struct Ty<'a, 'b>(&'a &'b ());

impl<'a, 'b> Ty<'a, 'b>{
    fn f() {
        None::<&'a &'b ()>;
        //~^ ERROR lifetime bound not satisfied
    }
}

The first repro regressed in 1.43 while the second one is as old as implied bounds.

@rustbot label A-implied-bounds A-associated-items regression-from-stable-to-stable T-types

Meta

Nightly version: 1.64.0-nightly

(2022-07-01 46b8c23)

Backtrace

<backtrace>

@aliemjay aliemjay added the C-bug Category: This is a bug. label Jul 3, 2022
@rustbot rustbot added A-associated-items Area: Associated items (types, constants & functions) A-implied-bounds Area: Implied bounds / inferred outlives-bounds regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-types Relevant to the types team, which will review and decide on the PR/issue. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jul 3, 2022
@apiraino
Copy link
Contributor

apiraino commented Jul 4, 2022

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-medium

@rustbot rustbot added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jul 4, 2022
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 8, 2023
fix fn item implied bounds and wf check

These are two distinct changes:
1. Wf-check all fn item substs.
Fixes rust-lang#104005

2. Use implied bounds from impl header.
Fixes rust-lang#98852
Fixes rust-lang#102611

The first is a breaking change and will likely have big impact without the the second one. See the first commit for how it breaks libstd.

Landing the second one without the first will allow more incorrect code to pass. For example an exploit of rust-lang#104005 would be as simple as:
```rust
use core::fmt::Display;

trait ExtendLt<Witness> {
    fn extend(self) -> Box<dyn Display>;
}

impl<T: Display> ExtendLt<&'static T> for T {
    fn extend(self) -> Box<dyn Display> {
        Box::new(self)
    }
}

fn main() {
    let val = (&String::new()).extend();
    println!("{val}");
}
```

cc `@lcnr`
r? types
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 9, 2023
fix fn item implied bounds and wf check

These are two distinct changes:
1. Wf-check all fn item substs.
Fixes rust-lang#104005

2. Use implied bounds from impl header.
Fixes rust-lang#98852
Fixes rust-lang#102611

The first is a breaking change and will likely have big impact without the the second one. See the first commit for how it breaks libstd.

Landing the second one without the first will allow more incorrect code to pass. For example an exploit of rust-lang#104005 would be as simple as:
```rust
use core::fmt::Display;

trait ExtendLt<Witness> {
    fn extend(self) -> Box<dyn Display>;
}

impl<T: Display> ExtendLt<&'static T> for T {
    fn extend(self) -> Box<dyn Display> {
        Box::new(self)
    }
}

fn main() {
    let val = (&String::new()).extend();
    println!("{val}");
}
```

cc `@lcnr`
r? types
@aliemjay
Copy link
Member Author

The same issue exists for associated consts:

/// A type with an implied bound `'b: 'a`
struct Ty<'a, 'b>(&'a &'b ());

impl<'a, 'b> Ty<'a, 'b> {
    const CONST_1: () = ();
    const CONST_2: () = {
        Self::CONST_1
        //~^ ERROR lifetime may not live long enough
    };
}

@aliemjay aliemjay changed the title implied bounds from impl header are not used in associated functions implied bounds from impl header are not used in associated functions/consts Nov 13, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 20, 2023
fix fn/const items implied bounds and wf check

These are two distinct changes (edit: actually three, see below):
1. Wf-check all fn item args. This is a soundness fix.
Fixes rust-lang#104005

2. Use implied bounds from impl header in borrowck of associated functions/consts. This strictly accepts more code and helps to mitigate the impact of other breaking changes.
Fixes rust-lang#98852
Fixes rust-lang#102611

The first is a breaking change and will likely have a big impact without the the second one. See the first commit for how it breaks libstd.

Landing the second one without the first will allow more incorrect code to pass. For example an exploit of rust-lang#104005 would be as simple as:
```rust
use core::fmt::Display;

trait ExtendLt<Witness> {
    fn extend(self) -> Box<dyn Display>;
}

impl<T: Display> ExtendLt<&'static T> for T {
    fn extend(self) -> Box<dyn Display> {
        Box::new(self)
    }
}

fn main() {
    let val = (&String::new()).extend();
    println!("{val}");
}
```

The third change is to to check WF of user type annotations before normalizing them (fixes rust-lang#104764, fixes rust-lang#104763). It is mutually dependent on the second change above: an attempt to land it separately in rust-lang#104746 caused several crater regressions that can all be mitigated by using the implied from the impl header. It is also necessary for the soundness of associated consts that use the implied bounds of impl header. See rust-lang#104763 and how the third commit fixes the soundness issue in `tests/ui/wf/wf-associated-const.rs` that was introduces by the previous commit.

cc `@lcnr`
r? types
@bors bors closed this as completed in 6bf600b Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-implied-bounds Area: Implied bounds / inferred outlives-bounds C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
3 participants