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

rework winnowing to sensibly handle global where-bounds #132325

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lcnr
Copy link
Contributor

@lcnr lcnr commented Oct 29, 2024

There may be multiple ways to prove a given trait-bound. In case there are multiple such applicable candidates we need to somehow merge them or bail with ambiguity. When merging candidates we prefer some over others for multiple reasons.

  • we want to guide inference during typeck, even if not strictly necessary
  • to avoid ambiguity if there if there are at most lifetime differences
    • old solver needs exactly one candidate
    • new solver only needs to handle lifetime differences
  • we disable normalization via impls if the goal is proven by using a where-bound

The approach in this PR1

  • always prefer trivial builtin impls2
  • then prefer non-global3 where-bounds
    • if there are no other where-bounds, guide inference
    • if there are any other where-bounds even if trivial, ambig
  • then prefer alias bounds4 and builtin trait object candidates54
  • merge everything ignoring trivial where-bounds, if there are no candidates, try using global where-bounds6

We disable normalization via impls when using non-global where-bounds or alias-bounds, even if we're unable to normalize by using the where-bound.

Why this behavior?

inference guidance via where-bounds and alias-bounds

where-bounds

fn method_selection<T: Into<u64>>(x: T) -> Option<u32> {
    x.into().try_into().ok()
    // prove `T: Into<?0>` and then select a method `?0`,
    // needs eager inference.
}

While the above pattern exists in the wild, I think that most inference guidance due to where-bounds is actually unintended. I believe we may want to restrict inference guidance in the future, e.g. limit it to where-bounds whose self-type is a param.

alias-bounds

pub trait Dyn {
    type Word: Into<u64>;
    fn d_tag(&self) -> Self::Word;
    fn tag32(&self) -> Option<u32> {
        self.d_tag().into().try_into().ok()
        // prove `Self::Word: Into<?0>` and then select a method
        // on `?0`, needs eager inference.
    }
}

Disable normalization via impls when using where-bounds

cc rust-lang/trait-system-refactor-initiative#125

trait Trait<'a> {
    type Assoc;
}

impl<T> Trait<'static> for T {
    type Assoc = ();
}

// normalizing requires `'a == 'static`, the trait bound does not.
fn foo<'a, T: Trait<'a>>(_: T::Assoc) {}

If an impl adds constraints not required by a where-bound, using the impl may cause compilation failure avoided by treating the associated type as rigid.

This is also why we can always use trivial builtin impls, even for normalization. They are guaranteed to never add any requirements.

Lower priority for global where-bounds

A where-bound is considered global if it does not refer to any generic parameters and is not higher-ranked. It may refer to 'static.

This means global where-bounds are either always fully implied by an impl or unsatisfiable. We don't really care about the inference behavior of unsatisfiable where-bounds :3

If a where-bound is fully implied then any using an applicable impl for normalization cannot result in additional constraints. As this is the - afaict only - reason why we disable normalization via impls in the first place, we don't have to disable normalization via impls when encountering global where-bounds.

Consider true global where-bounds at all

Given that we just use impls even if there exists a global where-bounds, you may ask why we don't just ignore these global where-bounds entirely: we use them to weaken the inference guidance from non-global where-bounds.

Without a global where-bound, we currently prefer non-global where bounds even though there would be an applicable impl as well. By adding a non-global where-bound, this unnecessary inference guidance is disabled, allowing the following to compile:

fn check<Color>(color: Color)
where
    Vec: Into<Color> + Into<f32>,
{
    let _: f32 = Vec.into();
    // Without the global `Vec: Into<f32>`  bound we'd
    // eagerly use the non-global `Vec: Into<Color>` bound
    // here, causing this to fail.
}

struct Vec;
impl From<Vec> for f32 {
    fn from(_: Vec) -> Self {
        loop {}
    }
}

There exist multiple crates which rely on this behavior.

Design considerations

We would like to be able to normalize via impls as much as possible. Disabling normalization simply because there exists a where-bound is undesirable.

For the sake of backwards compatability I intend to mostly mirror the current inference guidance rules and then explore possible improvements once the new solver is done. I do believe that removing unnecessary inference guide where possible is desirable however.

Whether a where-bound is global depends on whether used lifetimes are 'static. The where-bound u32: Trait<'static> is either entirely implied by an impl, meaning that it does not have to disable normalization via impls, while u32: Trait<'a> needs to disable normalization via impls as the impl may only hold for 'static. Considering all where-bounds to be non-global once they contain any region is unfortunately a breaking change.

How does this differ from stable

The currently stable approach is order dependent:

  • it prefers impls over global where-bounds: impl > global
  • it prefers non-global where-bounds over impls: non-global > impl
  • it treats all where-bounds equally: global = non-global

This means that whether we bail with ambiguity or simply use the non-global where bound depending on the order of where-clauses and number of applicable impl candidates. See the tests added in the first commit for more details. With this PR we now always bail with ambiguity.

I've previously tried to always use the non-global candidate, causing unnecessary inference guidance and undesirable breakage. This already went through an FCP in #124592. However, I consider the new approach to be preferable as it exclusively removes incompleteness. It also doesn't cause any crater breakage.

How to support this in the new solver :o

This is not implemented in this PR and not part of this FCP!

To implement the global vs non-global where-bound distinction, we have to either keep 'static in the param_env when canonicalizing, or eagerly distinguish global from non-global where-bounds and provide that information to the canonical query.

The old solver currently keeps 'static only the param_env, replacing it with an inference variable in the value.

let canonical_param_env = self.tcx.canonical_param_env_cache.get_or_insert(
self.tcx,
param_env,
query_state,
|tcx, param_env, query_state| {
// FIXME(#118965): We don't canonicalize the static lifetimes that appear in the
// `param_env` because they are treated differently by trait selection.
Canonicalizer::canonicalize(
param_env,
None,
tcx,
&CanonicalizeFreeRegionsOtherThanStatic,
query_state,
)
},
);

I dislike that based on vibes and it may end up being a problem once we extend the environment inside of the solver as we must not rely on 'static in the predicate as it would get erased in MIR typeck.

An alternative would be to eagerly detect trivial where-bounds when constructing the ParamEnv. We can't entirely drop them as explained above, so we'd instead replace them with a new clause kind TraitImpliedByImpl which gets entirely ignored except when checking whether we should eagerly guide inference via a where-bound. This approach can be extended to where-bounds which are currently not considered global to stop disabling normalization for them as well.

Keeping 'static in the param_env is the simpler solution here and we should be able to move to the second approach without any breakage. I therefore propose to keep 'static in the environment for now.


r? @compiler-errors

Footnotes

  1. see the source for more details

  2. they don't constrain inference and don't add any lifetime constraints

  3. a where-bound is global if it is not higher-ranked and doesn't contain any generic parameters, 'static is ok

  4. we arbitrary select a single object and alias-bound candidate in case multiple apply and they don't impact inference. This should be unnecessary in the new solver. 2

  5. Necessary for dyn Any and https://github.com/rust-lang/rust/issues/57893

  6. global where-bounds are only used if they are unsatisfiable, i.e. no impl candidate exists

@lcnr
Copy link
Contributor Author

lcnr commented Oct 29, 2024

@bors try @rust-timer queue

@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. labels Oct 29, 2024
@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 29, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 29, 2024
rework winnowing to sensibly handle global where-bounds

this is somewhat weird, but it at least allows us to mirror this behavior in the new solver:
- trivial builtin-impls
- non-global where-bounds, bailing with ambiguity if at least one global where-bound exists
- object ☠️ + alias-bound candidates
- merge candidates ignoring global where-bounds

r? `@compiler-errors`
@bors
Copy link
Contributor

bors commented Oct 29, 2024

⌛ Trying commit dc50c64 with merge 6d565b7...

@bors
Copy link
Contributor

bors commented Oct 29, 2024

☀️ Try build successful - checks-actions
Build commit: 6d565b7 (6d565b71a096078f59574161a2a382ec4a1eb751)

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (6d565b7): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.4% [-1.2%, -0.1%] 69
Improvements ✅
(secondary)
-1.2% [-2.2%, -0.7%] 3
All ❌✅ (primary) -0.4% [-1.2%, -0.1%] 69

Max RSS (memory usage)

Results (primary -2.2%, secondary 0.6%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.4% [1.7%, 5.6%] 4
Improvements ✅
(primary)
-2.2% [-3.2%, -1.2%] 4
Improvements ✅
(secondary)
-3.1% [-4.4%, -1.8%] 3
All ❌✅ (primary) -2.2% [-3.2%, -1.2%] 4

Cycles

Results (primary 2.3%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.3% [1.8%, 2.7%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.3% [1.8%, 2.7%] 2

Binary size

Results (primary -0.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.1%, -0.0%] 8
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.1%, -0.0%] 8

Bootstrap: 784.701s -> 783.947s (-0.10%)
Artifact size: 333.58 MiB -> 333.58 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 29, 2024
@lcnr
Copy link
Contributor Author

lcnr commented Oct 29, 2024

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-132325 created and queued.
🤖 Automatically detected try build 6d565b7
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 29, 2024
@craterbot
Copy link
Collaborator

🚧 Experiment pr-132325 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-132325 is completed!
📊 403 regressed and 4 fixed (532439 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the blacklist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Nov 6, 2024
@lcnr
Copy link
Contributor Author

lcnr commented Nov 7, 2024

@bors try

for later: check p=1 crates=https://crater-reports.s3.amazonaws.com/pr-132325/retry-regressed-list.txt

@bors
Copy link
Contributor

bors commented Nov 7, 2024

⌛ Trying commit 2090bf2 with merge 5be2110...

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 7, 2024
rework winnowing to sensibly handle global where-bounds

this is somewhat weird, but it at least allows us to mirror this behavior in the new solver:
- trivial builtin-impls
- non-global where-bounds, bailing with ambiguity if at least one global where-bound exists
- object ☠️ + alias-bound candidates
- merge candidates ignoring global where-bounds

This is a different approach from rust-lang#124592 which maintains the "if there are global where-bounds, don't guide type inference using non-global where-bounds" behavior, hopefully avoiding the breakage and means we use guidance from non-global where-bounds in fewer, instead of in more cases.

r? `@compiler-errors`
@bors
Copy link
Contributor

bors commented Nov 7, 2024

☀️ Try build successful - checks-actions
Build commit: 5be2110 (5be211015c49a4eb5c973cc5ebe17db5c6e7ffb8)

@compiler-errors
Copy link
Member

@craterbot
Copy link
Collaborator

👌 Experiment pr-132325-1 created and queued.
🤖 Automatically detected try build 5be2110
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 7, 2024
@craterbot
Copy link
Collaborator

🚧 Experiment pr-132325-1 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-132325-1 is completed!
📊 0 regressed and 0 fixed (988 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the blacklist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Nov 9, 2024
@lcnr lcnr force-pushed the damn-son branch 2 times, most recently from 60f8fc5 to 88bca6a Compare November 14, 2024 17:42
@rust-log-analyzer

This comment has been minimized.

@lcnr lcnr added T-types Relevant to the types team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 14, 2024
@lcnr
Copy link
Contributor Author

lcnr commented Nov 14, 2024

hello there :3

see the PR description for an in-depth summary of what's going on there. The actual changes in this PR are very limited, see the "How does this differ from stable" section.

@rfcbot fcp merge

@lcnr lcnr closed this Nov 14, 2024
@lcnr lcnr reopened this Nov 14, 2024
@lcnr

This comment was marked as off-topic.

@rfcbot
Copy link

rfcbot commented Nov 14, 2024

Team member @lcnr has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants