Skip to content

Rust thinks std::marker::Sized differ from std::marker::Sized #44835

Closed
@Zoxc

Description

@Zoxc
trait DescriptiveSpec<'r> {}

impl<'r, T> DescriptiveSpec<'r> for &'r T {}

fn from_spec<'r, T: DescriptiveSpec<'r>>(spec: &'r T)  {}

fn matching_contains<'s, T: 's, I>(a: &mut &'s I) where &'s I: Sized {
    from_spec(a);
}

fn main() {}

gives this error

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'r` due to conflicting requirements
 --> src/main.rs:9:5
  |
9 |     from_spec(a);
  |     ^^^^^^^^^
  |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the function body at 8:1...
 --> src/main.rs:8:1
  |
8 | / fn matching_contains<'s, T: 's, I>(a: &mut &'s I) where &'s I: Sized {
9 | |     from_spec(a);
10| | }
  | |_^
note: ...so that reference does not outlive borrowed content
 --> src/main.rs:9:15
  |
9 |     from_spec(a);
  |               ^
note: but, the lifetime must be valid for the lifetime 's as defined on the function body at 8:1...
 --> src/main.rs:8:1
  |
8 | / fn matching_contains<'s, T: 's, I>(a: &mut &'s I) where &'s I: Sized {
9 | |     from_spec(a);
10| | }
  | |_^
note: ...so that types are compatible (expected std::marker::Sized, found std::marker::Sized)
 --> src/main.rs:9:5
  |
9 |     from_spec(a);
  |     ^^^^^^^^^

The note ...so that types are compatible (expected std::marker::Sized, found std::marker::Sized) does not seem correct. This error is likely due to the function having two &'a I: Sized bounds where 's have two different lifetimes, see #21974.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions