Skip to content

Avoid impossible type/region/const Bound combinations - #165840

Open
nnethercote wants to merge 1 commit into
rust-lang:mainfrom
nnethercote:avoid-impossible-bound-combinations
Open

nnethercote wants to merge 1 commit into
rust-lang:mainfrom
nnethercote:avoid-impossible-bound-combinations

Conversation

@nnethercote

Copy link
Copy Markdown
Contributor

TyKind has this variant:

Bound(BoundVarIndexKind, ty::BoundTy<I>),

The relevant types within are as follow:

enum BoundVarIndexKind {
    Bound(DebruijnIndex),
    Canonical,
}

struct BoundTy<I: Interner> {
    pub var: ty::BoundVar,
    pub kind: BoundTyKind<I>,
}

pub enum BoundTyKind<I: Interner> {
    Anon,
    Param(I::DefId),
}

The problem here is that BoundTyKind doesn't really apply with BoundVarIndexKind::Canonical, so it's always set to BoundTyKind::Anon, but the types don't enforce this.

It took me a long time to realise this, and got in the way of my understanding of Bound vs. Canonical.

This commit changes TyKind's variant to this:

Bound(BoundVarIndexKind<ty::BoundTy<I>>),

And BoundVarIndexKind to this:

pub enum BoundVarIndexKind<B> {
    Bound(DebruijnIndex, B),
    Canonical(BoundVar),
}

The BoundTy is now inside the BoundVarIndexKind::Bound. And BoundVarIndexKind::Canonical only contains a BoundVar, which means it can't have an inappropriate BoundTyKind.

There is a similar story for RegionKind::ReBound and ConstKind::Bound, and this commit changes them as well.

Note: BoundConst debug printing also changed a little; it's now more like BoundTy's.

r? @jackh726

`TyKind` has this variant:
```
Bound(BoundVarIndexKind, ty::BoundTy<I>),
```
The relevant types within are as follow:
```
enum BoundVarIndexKind {
    Bound(DebruijnIndex),
    Canonical,
}

struct BoundTy<I: Interner> {
    pub var: ty::BoundVar,
    pub kind: BoundTyKind<I>,
}

pub enum BoundTyKind<I: Interner> {
    Anon,
    Param(I::DefId),
}
```
The problem here is that `BoundTyKind` doesn't really apply with
`BoundVarIndexKind::Canonical`, so it's always set to
`BoundTyKind::Anon`, but the types don't enforce this.

It took me a long time to realise this, and got in the way of my
understanding of `Bound` vs. `Canonical`.

This commit changes `TyKind`'s variant to this:
```
Bound(BoundVarIndexKind<ty::BoundTy<I>>),
```
And `BoundVarIndexKind` to this:
```
pub enum BoundVarIndexKind<B> {
    Bound(DebruijnIndex, B),
    Canonical(BoundVar),
}
```
The `BoundTy` is now inside the `BoundVarIndexKind::Bound`. And
`BoundVarIndexKind::Canonical` only contains a `BoundVar`, which means
it can't have an inappropriate `BoundTyKind`.

There is a similar story for `RegionKind::ReBound` and
`ConstKind::Bound`, and this commit changes them as well.

Note: `BoundConst` debug printing also changed a little; it's now more
like `BoundTy`'s.
@rustbot

rustbot commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in const_evaluatable.rs

cc @BoxyUwU

HIR ty lowering was modified

cc @fmease

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

Some changes occurred in exhaustiveness checking

cc @Nadrieril

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

clippy is developed in its own repository. If possible, consider making this change to rust-lang/rust-clippy instead.

cc @rust-lang/clippy

Some changes occurred in compiler/rustc_sanitizers

cc @rcvalle

@rustbot rustbot added PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Nov 12, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

Shouldn't affect perf, let's check:

@bors try @rust-timer queue

@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 Nov 12, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 12, 2026
…ns, r=<try>

Avoid impossible type/region/const `Bound` combinations
@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: bed2628 (bed2628503868edceb0f948d5822d64236fc08dd)
Base parent: ee8268e (ee8268e23d0e2e308b52e4f63df69afb69bc61a4)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (bed2628): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (primary 9.1%, secondary -3.6%)

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

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

Cycles

Results (secondary 2.3%)

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

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

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 484.606s -> 485.041s (0.09%)
Artifact size: 406.06 MiB -> 406.01 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 12, 2026
@nnethercote

Copy link
Copy Markdown
Contributor Author

Perf is neutral.

@jackh726

Copy link
Copy Markdown
Member

Pretty mildly oppose to implementation, although I'm sympathetic to the idea.

Today a bound ty can essentially be thought of

  1. at what depth is it bound (which may be "no depth" aka canonical)
  2. which variable at depth
  3. some extra info that its origin for diagnostics

the proposed change really obfuscates that mainly because the BoundVarIndexKind type takes this B parameter that hides the BoundVar.

If we're going to do this, then I would want to just make BoundTy a non-generic enum directly, possibly splitting BoundVar out into TyKind::Bound.

@nnethercote

Copy link
Copy Markdown
Contributor Author

Pretty mildly oppose to implementation, although I'm sympathetic to the idea.

Today a bound ty can essentially be thought of

1. at what depth is it bound (which may be "no depth" aka canonical)

2. which variable at depth

3. some extra info that its origin for diagnostics

But 3 doesn't really apply in the canonical case, right? That's the whole point of this PR.

If we're going to do this, then I would want to just make BoundTy a non-generic enum directly, possibly splitting BoundVar out into TyKind::Bound.

I tried something like that originally. It required more mechanical changes, and there's a bigger problem: PlaceholderType contains a BoundTy and that one probably cannot/should not change. So you still have BoundTy and then also some new type.

Another design I played with was introducing a new TyKind variant: TyKind::Canon(BoundVar). And then we get TyKind::Bound(DebruijnIndex, BoundTy). I kind of like this because to me canonicals feel quite different to bound vars... a bit like ReErased, in that it's a temporarily special value used when we want to just ignore certain information. But it does require a lot of top-level matches to have ... | ty::Bound(..) | ty::Canon(_) => ....

@jackh726

Copy link
Copy Markdown
Member

But 3 doesn't really apply in the canonical case, right? That's the whole point of this PR.

Correct, but it's much more important to treat bound vars as similar regardless if they bound at a given depth or canonical than it is to distinguish between whether a bound var can have different types of debug info.

I tried something like that originally. It required more mechanical changes, and there's a bigger problem: PlaceholderType contains a BoundTy and that one probably cannot/should not change. So you still have BoundTy and then also some new type.

And this is a clear indication that this type of change is likely not worth it. Though, it may be okay for ty::Placeholder to contain the BoundVar then be generic over the *Kind.

Another design I played with was introducing a new TyKind variant: TyKind::Canon(BoundVar).

I don't really like this option, either.

I kind of like this because to me canonicals feel quite different to bound vars... a bit like ReErased, in that it's a temporarily special value used when we want to just ignore certain information.

This isn't true - bound vars have very similar logical meaning whether they are canonical or not.

}
ty::Pat(..) => unreachable!(),
ty::Bound(_, _) => bug!("bound ty during ctfe"),
ty::Bound(_) => bug!("bound ty during ctfe"),

@RalfJung RalfJung Nov 15, 2026 •

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.

Suggested change
ty::Bound(_) => bug!("bound ty during ctfe"),
ty::Bound(..) => bug!("bound ty during ctfe"),

View changes since the review

@rust-bors

rust-bors Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #166152) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

This branch has not been deployed

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

Labels

PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc 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.

5 participants