Skip to content

Tracking issue for IMPLIED_BOUNDS_ENTAILMENT lint #105572

Closed

Description

This is a tracking issue for the IMPLIED_BOUNDS_ENTAILMENT lint, which was added in #105575. The lint detects cases where the arguments of an impl method have stronger implied bounds than those from the trait method it's implementing.

Example

This warning will trigger for code like:

#![deny(implied_bounds_entailment)]

use std::borrow::Cow;

pub trait Decoder { type Error; }

pub trait Decodable: Sized {
   fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error>;
}

impl<'a, T: ?Sized> Decodable for Cow<'a, T>
where T: ToOwned, T::Owned: Decodable
{
    fn decode<D: Decoder>(d: &mut D) -> Result<Cow<'static, T>, D::Error> {
        Ok(Cow::Owned(Decodable::decode(d)?))
    }
}

Unsoundness Exploitation

This can be used to implement an unsound API if used incorrectly, see #80176. While the previous example is sound, it can result in undefined behavior not previously caught by the compiler:

#![deny(implied_bounds_entailment)]

trait Trait {
    fn get<'s>(s: &'s str, _: &'static &'static ()) -> &'static str;
}

impl Trait for () {
    fn get<'s>(s: &'s str, _: &'static &'s ()) -> &'static str {
        s
    }
}

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

Explanation

Neither the trait method, which provides no implied bounds about 's, nor the impl, which can't name 's, requires the main function to prove that 's: 'static, but the impl method is able to assume that 's: 'static within its own body.


See: #105483, #105295

bumped to deny-by-default in #106465

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: A tracking issue for an RFC or an unstable feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions