Skip to content

hir_analysis: add missing sizedness bounds #142712

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

davidtwco
Copy link
Member

Default sizedness bounds were not being added to explicit_super_predicates_of and explicit_implied_predicates_of which meant that a trait bound added to a associated type projection would be missing the implied predicate of the default sizedness supertrait of that trait.

An unexpected consequence of this change was that the check for multiple principals was now finding an additional MetaSized principal when eagerly expanding trait aliases - this required modifying lowering to no longer add default bounds to trait aliases.

Default sizedness bounds were not being added to
`explicit_super_predicates_of` and `explicit_implied_predicates_of`
which meant that a trait bound added to a associated type projection
would be missing the implied predicate of the default sizedness
supertrait of that trait.

An unexpected consequence of this change was that the check for multiple
principals was now finding an additional `MetaSized` principal when
eagerly expanding trait aliases - this required modifying lowering to no
longer add default bounds to trait aliases.
@rustbot
Copy link
Collaborator

rustbot commented Jun 19, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jun 19, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 19, 2025

HIR ty lowering was modified

cc @fmease

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-tools failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Compiling fluent-langneg v0.13.0
error[E0308]: mismatched types
  --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:96:39
   |
96 |     <K as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K as ZeroMapKV<'s>>::Container>,
   |                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected trait `<<K as kv::ZeroMapKV<'s>>::Container as MetaSized>`
              found trait `<<K as kv::ZeroMapKV<'zf>>::Container as MetaSized>`
note: the lifetime `'zf` as defined here...
  --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:92:6
   |
92 | impl<'zf, 's, K, V> ZeroFrom<'zf, ZeroMap<'s, K, V>> for ZeroMap<'zf, K, V>
   |      ^^^
note: ...does not necessarily outlive the lifetime `'s` as defined here
  --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:92:11
   |
92 | impl<'zf, 's, K, V> ZeroFrom<'zf, ZeroMap<'s, K, V>> for ZeroMap<'zf, K, V>
   |           ^^

error[E0308]: mismatched types
  --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:96:39
   |
96 |     <K as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K as ZeroMapKV<'s>>::Container>,
   |                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected trait `<<K as kv::ZeroMapKV<'s>>::Container as MetaSized>`
              found trait `<<K as kv::ZeroMapKV<'zf>>::Container as MetaSized>`
note: the lifetime `'s` as defined here...
  --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:92:11
   |
92 | impl<'zf, 's, K, V> ZeroFrom<'zf, ZeroMap<'s, K, V>> for ZeroMap<'zf, K, V>
   |           ^^
note: ...does not necessarily outlive the lifetime `'zf` as defined here
  --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:92:6
   |
92 | impl<'zf, 's, K, V> ZeroFrom<'zf, ZeroMap<'s, K, V>> for ZeroMap<'zf, K, V>
   |      ^^^

error[E0308]: mismatched types
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:112:40
    |
112 |     <K0 as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K0 as ZeroMapKV<'s>>::Container>,
    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
    |
    = note: expected trait `<<K0 as kv::ZeroMapKV<'s>>::Container as MetaSized>`
               found trait `<<K0 as kv::ZeroMapKV<'zf>>::Container as MetaSized>`
note: the lifetime `'zf` as defined here...
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:107:6
    |
107 | impl<'zf, 's, K0, K1, V> ZeroFrom<'zf, ZeroMap2d<'s, K0, K1, V>> for ZeroMap2d<'zf, K0, K1, V>
    |      ^^^
note: ...does not necessarily outlive the lifetime `'s` as defined here
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:107:11
    |
107 | impl<'zf, 's, K0, K1, V> ZeroFrom<'zf, ZeroMap2d<'s, K0, K1, V>> for ZeroMap2d<'zf, K0, K1, V>
    |           ^^

error[E0308]: mismatched types
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:112:40
    |
112 |     <K0 as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K0 as ZeroMapKV<'s>>::Container>,
    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
    |
    = note: expected trait `<<K0 as kv::ZeroMapKV<'s>>::Container as MetaSized>`
               found trait `<<K0 as kv::ZeroMapKV<'zf>>::Container as MetaSized>`
note: the lifetime `'s` as defined here...
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:107:11
    |
107 | impl<'zf, 's, K0, K1, V> ZeroFrom<'zf, ZeroMap2d<'s, K0, K1, V>> for ZeroMap2d<'zf, K0, K1, V>
    |           ^^
note: ...does not necessarily outlive the lifetime `'zf` as defined here
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:107:6
    |
107 | impl<'zf, 's, K0, K1, V> ZeroFrom<'zf, ZeroMap2d<'s, K0, K1, V>> for ZeroMap2d<'zf, K0, K1, V>
    |      ^^^

[RUSTC-TIMING] unic_langid test:false 0.068
[RUSTC-TIMING] unic_langid_macros test:false 0.077
   Compiling intl_pluralrules v7.0.2
---
   Compiling darling_macro v0.20.11
error[E0521]: borrowed data escapes outside of associated function
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/yoke_impls.rs:138:20
    |
107 | unsafe impl<'a, K, V> Yokeable<'a> for ZeroMap<'static, K, V>
    |             -- lifetime `'a` defined here
...
136 |     unsafe fn make(from: Self::Output) -> Self {
    |                    ----
    |                    |
    |                    `from` declared here, outside of the associated function body
    |                    `from` is a reference that is only valid in the associated function body
137 |         debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
138 |         let from = mem::ManuallyDrop::new(from);
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                    |
    |                    `from` escapes the associated function body here
    |                    argument requires that `'a` must outlive `'static`
    |
    = note: requirement occurs because of the type `ZeroMap<'_, K, V>`, which makes the generic argument `'_` invariant
    = note: the struct `ZeroMap<'a, K, V>` is invariant over the parameter `'a`
    = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

error[E0521]: borrowed data escapes outside of associated function
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/yoke_impls.rs:232:20
    |
199 | unsafe impl<'a, K0, K1, V> Yokeable<'a> for ZeroMap2d<'static, K0, K1, V>
    |             -- lifetime `'a` defined here
...
230 |     unsafe fn make(from: Self::Output) -> Self {
    |                    ----
    |                    |
    |                    `from` declared here, outside of the associated function body
    |                    `from` is a reference that is only valid in the associated function body
231 |         debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
232 |         let from = mem::ManuallyDrop::new(from);
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                    |
    |                    `from` escapes the associated function body here
    |                    argument requires that `'a` must outlive `'static`
    |
    = note: requirement occurs because of the type `ZeroMap2d<'_, K0, K1, V>`, which makes the generic argument `'_` invariant
    = note: the struct `ZeroMap2d<'a, K0, K1, V>` is invariant over the parameter `'a`
    = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

error: lifetime may not live long enough
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:101:19
    |
92  | impl<'zf, 's, K, V> ZeroFrom<'zf, ZeroMap<'s, K, V>> for ZeroMap<'zf, K, V>
    |      ---  -- lifetime `'s` defined here
    |      |
    |      lifetime `'zf` defined here
...
101 |             keys: K::Container::zero_from(&other.keys),
    |                   ^^^^^^^^^^^^^^^^^^^^^^^ requires that `'zf` must outlive `'s`
    |
    = help: consider adding the following bound: `'zf: 's`

error: lifetime may not live long enough
   --> /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/zerofrom_impls.rs:118:20
    |
107 | impl<'zf, 's, K0, K1, V> ZeroFrom<'zf, ZeroMap2d<'s, K0, K1, V>> for ZeroMap2d<'zf, K0, K1, V>
    |      ---  -- lifetime `'s` defined here
    |      |
    |      lifetime `'zf` defined here
...
118 |             keys0: K0::Container::zero_from(&other.keys0),
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'zf` must outlive `'s`
    |
    = help: consider adding the following bound: `'zf: 's`

Some errors have detailed explanations: E0308, E0521.
For more information about an error, try `rustc --explain E0308`.
[RUSTC-TIMING] zerovec test:false 2.767
error: could not compile `zerovec` (lib) due to 8 previous errors

@davidtwco
Copy link
Member Author

Wasn't expecting that failure, looking into it.

@davidtwco
Copy link
Member Author

davidtwco commented Jun 19, 2025

A smaller example of one of the failing crates (which happens with a stage two build):

#![crate_type = "lib"]

use std::marker::PhantomData;

pub trait ZeroMapKV<'a> {
    type Container;
}

pub trait ZeroFrom<'zf, C: ?Sized> {}

pub struct ZeroMap<'a, K: ZeroMapKV<'a>>(PhantomData<&'a K>);

impl<'zf, 's, K> ZeroFrom<'zf, ZeroMap<'s, K>> for ZeroMap<'zf, K>
where
    K: for<'b> ZeroMapKV<'b>,
    <K as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K as ZeroMapKV<'s>>::Container>,
{
}
error[E0308]: mismatched types
  --> f.rs:16:39
   |
16 |     <K as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K as ZeroMapKV<'s>>::Container>,
   |                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected trait `<<K as ZeroMapKV<'s>>::Container as MetaSized>`
              found trait `<<K as ZeroMapKV<'zf>>::Container as MetaSized>`
note: the lifetime `'zf` as defined here...
  --> f.rs:13:6
   |
13 | impl<'zf, 's, K> ZeroFrom<'zf, ZeroMap<'s, K>> for ZeroMap<'zf, K>
   |      ^^^
note: ...does not necessarily outlive the lifetime `'s` as defined here
  --> f.rs:13:11
   |
13 | impl<'zf, 's, K> ZeroFrom<'zf, ZeroMap<'s, K>> for ZeroMap<'zf, K>
   |           ^^

error[E0308]: mismatched types
  --> f.rs:16:39
   |
16 |     <K as ZeroMapKV<'zf>>::Container: ZeroFrom<'zf, <K as ZeroMapKV<'s>>::Container>,
   |                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected trait `<<K as ZeroMapKV<'s>>::Container as MetaSized>`
              found trait `<<K as ZeroMapKV<'zf>>::Container as MetaSized>`
note: the lifetime `'s` as defined here...
  --> f.rs:13:11
   |
13 | impl<'zf, 's, K> ZeroFrom<'zf, ZeroMap<'s, K>> for ZeroMap<'zf, K>
   |           ^^
note: ...does not necessarily outlive the lifetime `'zf` as defined here
  --> f.rs:13:6
   |
13 | impl<'zf, 's, K> ZeroFrom<'zf, ZeroMap<'s, K>> for ZeroMap<'zf, K>
   |      ^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

@petrochenkov
Copy link
Contributor

r? types

@rustbot rustbot added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Jun 19, 2025
@rustbot rustbot assigned jackh726 and unassigned petrochenkov Jun 19, 2025
@compiler-errors
Copy link
Member

I'd like to think about this...

Can you remind me, is trait Foo {} actually trait Foo: MetaSized {}?

Copy link
Member

@fmease fmease Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would be worth adding the following regression test, too? It's probably redundant but it would be smaller:

#![feature(sized_hierarchy)]

trait Trait {}

fn f<T: Trait + std::marker::PointeeSized>() {}

On master it yields the error

error[E0277]: the size for values of type `T` cannot be known
 --> t.rs:5:9
  |
5 | fn f<T: Trait + std::marker::PointeeSized>() {}
  |         ^^^^^ doesn't have a known size

Trait should still imply MetaSized here in f's bounds, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants