Skip to content

Trait aliases: Also imply default trait bounds on type params other than Self#152688

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
fmease:implied-preds-default-bounds
Apr 14, 2026
Merged

Trait aliases: Also imply default trait bounds on type params other than Self#152688
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
fmease:implied-preds-default-bounds

Conversation

@fmease
Copy link
Copy Markdown
Member

@fmease fmease commented Feb 16, 2026

View all comments

Trait aliases already correctly imply default trait bounds on Self type params. However, due to an oversight, they didn't do that for normal type params.

Fixes #152687.

@fmease fmease added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Feb 16, 2026
@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Feb 16, 2026
@fmease fmease added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Feb 16, 2026
@fmease
Copy link
Copy Markdown
Member Author

fmease commented Feb 16, 2026

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 16, 2026
rust-bors Bot pushed a commit that referenced this pull request Feb 16, 2026
[WIP] Trait aliases: Imply default trait bounds
@rust-log-analyzer

This comment has been minimized.

@fmease fmease force-pushed the implied-preds-default-bounds branch 2 times, most recently from eff5e38 to fdb25e1 Compare February 16, 2026 02:21
@fmease fmease added the rla-silenced Silences rust-log-analyzer postings to the PR it's added on. label Feb 16, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Feb 16, 2026

☀️ Try build successful (CI)
Build commit: ffd787c (ffd787cdc83d953ee66126e6f9b32b4465636845, parent: 139651428df86cf88443295542c12ea617cbb587)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (ffd787c): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

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

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (primary 2.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.6% [2.6%, 2.6%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.6% [2.6%, 2.6%] 1

Cycles

Results (primary 0.2%, secondary 2.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.6% [2.6%, 2.6%] 1
Regressions ❌
(secondary)
2.6% [2.6%, 2.6%] 1
Improvements ✅
(primary)
-2.2% [-2.2%, -2.2%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [-2.2%, 2.6%] 2

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 482.657s -> 481.709s (-0.20%)
Artifact size: 397.96 MiB -> 395.94 MiB (-0.51%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 16, 2026
@fmease fmease changed the title [WIP] Trait aliases: Imply default trait bounds [WIP] Trait aliases: Imply default trait bounds on type params other than Self Feb 16, 2026
@fmease fmease force-pushed the implied-preds-default-bounds branch 2 times, most recently from 7269e75 to f61aed1 Compare February 16, 2026 15:19
@fmease fmease changed the title [WIP] Trait aliases: Imply default trait bounds on type params other than Self Trait aliases: Imply default trait bounds on type params other than Self Feb 16, 2026
@fmease fmease added F-trait_alias `#![feature(trait_alias)]` and removed rla-silenced Silences rust-log-analyzer postings to the PR it's added on. labels Feb 16, 2026
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

My change only affects trait aliases since implied_predicates_with_filter is only ever called with filter PredicateFilter::All for trait aliases (namely in explicit_implied_predicates_of). Consequently, my PR won't affect stable behavior.

) -> Vec<(ty::Clause<'tcx>, Span)> {
let mut bounds = Vec::new();

if let PredicateFilter::All = filter {
Copy link
Copy Markdown
Member Author

@fmease fmease Feb 16, 2026

Choose a reason for hiding this comment

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

Predicates where the bounded type is a HIR type param obviously never qualify for PredicateFilter::{SelfOnly, SelfTraitThatDefines, SelfAndAssociatedTypeBounds, SelfConstIfConst}.

Regarding PredicateFilter::{SelfConstIfConst, ConstIfConst}, (1) these shouldn't be collected here anyway (const conditions are gathered elsewhere), (2) we don't have any default const or const-if-const trait bounds at the time of writing. Sure, we might have const Sized and const MetaSized in the future (open RFC 3729 | Sized Hierarchy) but then that should be dealt with when that part gets implemented imo.

As a result, PredicateFilter::All is the only filter that makes sense.

trait B1<T: ?Sized> =; // has a default `T: MetaSized` bound
fn g1<T: std::marker::PointeeSized>() where (): B1<T> {}

// For completeness, let's also check default trait bounds on `Self`.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We do have tests that exercise add_implicit_sizedness_bounds in implied_predicates_with_filter, namely tests/ui/sized-hierarchy/bound-on-assoc-type-projection.rs and
tests/ui/sized-hierarchy/elaboration-simple.rs. However a trait alias specific one doesn't hurt imo.

@fmease
Copy link
Copy Markdown
Member Author

fmease commented Feb 16, 2026

r? types

@fmease fmease marked this pull request as ready for review February 16, 2026 15:35
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 16, 2026
Comment thread tests/ui/traits/alias/default-trait-bounds.rs
@fmease fmease changed the title Trait aliases: Imply default trait bounds on type params other than Self Trait aliases: Also imply default trait bounds on type params other than Self Feb 16, 2026
@fmease
Copy link
Copy Markdown
Member Author

fmease commented Apr 8, 2026

@jackh726 Friendly ping, it's been 7 weeks and 3 days. The changes are trivial btw.

Copy link
Copy Markdown
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

Apologies for the late review. I've been constantly trying to catch up on these.

So. do you know what PR added this implied behavior? I...almost think it's simpler/better to not imply default or explicit bounds on trait alias parameters. Perhaps, it's okay by me to land this, but I'd like to have a note of this behavior somewhere to ensure it gets revisited whenever we actually move towards trait alias stabilization.

Other, this looks good. Agreed that adding a test of the negative behavior here is good.

View changes since this review

Comment thread tests/ui/traits/alias/default-trait-bounds.rs

#![feature(trait_alias, more_maybe_bounds, lang_items, auto_traits)]

#[lang = "default_trait1"]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ha, this is funny that this exists.

@jackh726 jackh726 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 11, 2026
@fmease
Copy link
Copy Markdown
Member Author

fmease commented Apr 12, 2026

I...almost think it's simpler/better to not imply default or explicit bounds on trait alias parameters. Perhaps, it's okay by me to land this, but I'd like to have a note of this behavior somewhere to ensure it gets revisited whenever we actually move towards trait alias stabilization.

I do agree that it's odd and surprising that parameter bounds get implied but I didn't feel like submitting a PR that drastically altered the semantics of trait aliases either, esp. since they're in limbo design-wise.

However, if we were to stop implying parameter bounds, I think we should stop implying any predicates whose self type isn't Self (or a projection thereof), i.e., copy the behavior of normal traits. Otherwise trait A<T> = where T: Copy; and trait A<T> = where (T,): Copy; would behave differently. Of course, that would diminish the power of trait aliases but maybe that'd be a good thing.

So. do you know what PR added this implied behavior?

No, I haven't checked yet, I might do so later.

Of course, the behavior is perfectly consistent right now (apart from default bounds) since it's very intentional I'm pretty sure that given e.g., trait A = where for<'a> i32: Copy; (i.e., arbitrary bounds) trait alias ref A implies for<'a> i32: Copy (at least the behavior is well-known by users & devs and "PredicateFilter::All" says it all). Consequently trait A<T: Bound> =; should indeed imply T: Bound under these rules since it's equivalent to trait A<T> = where T: Bound;.

(I do wonder though if trait aliases used to behave differently wrt. parameter bounds when they were first added. Maybe <T: Bound> used to behave differently from <T> where T: Bound since they used to not share the same representation as you know 🤔)

(I remember Niko expressing his disappointment in some trait alias RFC thread regarding the fact that in Rust we're syntactically conflating ensures and requires by only utilizing : and where which are context dependent; trait X<T ensures Copy> =; vs. trait X<T requires Copy> =; would be grand albeit too lengthy ofc)

@fmease fmease force-pushed the implied-preds-default-bounds branch from f61aed1 to dce0935 Compare April 13, 2026 15:28
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 13, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@fmease fmease force-pushed the implied-preds-default-bounds branch from dce0935 to 9d627dd Compare April 13, 2026 15:35
@fmease
Copy link
Copy Markdown
Member Author

fmease commented Apr 13, 2026

I'd like to have a note of this behavior somewhere to ensure it gets revisited whenever we actually move towards trait alias stabilization.

I'm gonna add it to the tracking issue #41517 as an unresolved question.


Added tests for the negative behavior.

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 13, 2026
@jackh726
Copy link
Copy Markdown
Member

Thanks!

@bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 13, 2026

📌 Commit 9d627dd has been approved by jackh726

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 13, 2026
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@bors rollup
I think this can be rolled up, right?

rust-bors Bot pushed a commit that referenced this pull request Apr 14, 2026
Rollup of 5 pull requests

Successful merges:

 - #149357 (Implement `-Z allow-partial-mitigations` (RFC 3855))
 - #154939 (Refactor: simplify report_selection_error)
 - #152688 (Trait aliases: Also imply default trait bounds on type params other than `Self`)
 - #154352 (rustdoc: dep-info for standalone markdown inputs)
 - #155195 (tidy: handle `#[cfg_attr(bootstrap, doc = "...")]` in `compiler/` comments)
@rust-bors rust-bors Bot merged commit 7e40efc into rust-lang:main Apr 14, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 14, 2026
rust-timer added a commit that referenced this pull request Apr 14, 2026
Rollup merge of #152688 - fmease:implied-preds-default-bounds, r=jackh726

Trait aliases: Also imply default trait bounds on type params other than `Self`

Trait aliases already correctly imply default trait bounds on `Self` type params. However, due to an oversight, they didn't do that for normal type params.

Fixes #152687.
@fmease fmease deleted the implied-preds-default-bounds branch April 14, 2026 12:16
@fmease fmease mentioned this pull request Apr 14, 2026
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-trait_alias `#![feature(trait_alias)]` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trait aliases don't imply default trait bounds on type params other than Self

6 participants