Skip to content

Implement MVP for opaque generic const arguments#150823

Open
camelid wants to merge 5 commits intorust-lang:mainfrom
camelid:ogca
Open

Implement MVP for opaque generic const arguments#150823
camelid wants to merge 5 commits intorust-lang:mainfrom
camelid:ogca

Conversation

@camelid
Copy link
Member

@camelid camelid commented Jan 8, 2026

This is meant to be the interim successor to generic const expressions.
Essentially, const item RHS's will be allowed to do arbitrary const
operations using generics. The limitation is that these const items will
be treated opaquely, like ADTs in nominal typing, such that uses of them
will only be equal if the same const item is referenced. In other words,
two const items with the exact same RHS will not be considered equal.

I also added some logic to check feature gates that depend on others
being enabled (like oGCA depending on mGCA).

r? @BoxyUwU

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

camelid commented Jan 8, 2026

TODO:

  • only use AnonConstKind::OGCA for const item RHS's
  • add tests
  • (less urgent:) don't normalize opaque const items (those with OGCA RHS)
  • handle coherence correctly (ambiguous if not definitionally equal)

@camelid camelid added the A-const-generics Area: const generics (parameters and arguments) label Jan 8, 2026
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU
Copy link
Member

BoxyUwU commented Jan 8, 2026

When we avoid normalizing type consts to opaque anon consts we need to make sure that the normalization is ambiguous in coherence instead of NoSolution:

#![feature(min_generic_const_args, opaque_generic_const_args)]

#[type_const]
const FOO<const N: usize>: usize = const { N + 1 };

#[type_const]
const BAR<const N: usize>: usize = const { N + 1 };

trait Trait {}

impl Trait for [(); FOO::<1>] {}
impl Trait for [(); BAR::<1>] {}

ie we don't want this code to pass due to the two opaque anon consts being unequal.

@camelid
Copy link
Member Author

camelid commented Jan 8, 2026

Ooh good point 👍

@rust-log-analyzer

This comment has been minimized.

@camelid camelid marked this pull request as ready for review January 16, 2026 03:37
@rustbot
Copy link
Collaborator

rustbot commented Jan 16, 2026

HIR ty lowering was modified

cc @fmease

@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 Jan 16, 2026
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU
Copy link
Member

BoxyUwU commented Jan 16, 2026

also need to add the coherence test :3

@BoxyUwU BoxyUwU 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 Jan 16, 2026
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) label Jan 30, 2026
This is meant to be the interim successor to generic const expressions.
Essentially, const item RHS's will be allowed to do arbitrary const
operations using generics. The limitation is that these const items will
be treated opaquely, like ADTs in nominal typing, such that uses of them
will only be equal if the same const item is referenced. In other words,
two const items with the exact same RHS will not be considered equal.

I also added some logic to check feature gates that depend on others
being enabled (like oGCA depending on mGCA).
@rust-log-analyzer

This comment has been minimized.

During coherence, OGCA consts should be normalized ambiguously because
they are opaque but eventually resolved to a real value. We don't want
two OGCAs that have the same value to be treated as distinct for
coherence purposes. (Just like opaque types.)
@rustbot
Copy link
Collaborator

rustbot commented Feb 1, 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.

@camelid camelid 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 1, 2026
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-20-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
fmt: checked 6688 files
tidy check
tidy [rustdoc_json (src)]: `rustdoc-json-types` modified, checking format version
tidy: Skipping binary file check, read-only filesystem
tidy [style (compiler)]: /checkout/compiler/rustc_feature/src/unstable.rs:605: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
tidy [style (compiler)]: FAIL
tidy [style (tests)]: /checkout/tests/ui/const-generics/associated-const-bindings/dyn-compat-self-const-projections-in-assoc-const-ty.rs:7: line longer than 100 chars
tidy [style (tests)]: FAIL
tidy [features]: /checkout/compiler/rustc_feature/src/unstable.rs:606: no tracking issue for feature opaque_generic_const_args
tidy [features]: FAIL
tidy: The following checks failed: features, style (compiler), style (tests)
Bootstrap failed while executing `--stage 2 test --skip tests --skip coverage-map --skip coverage-run --skip library --skip tidyselftest`
Command `/checkout/obj/build/aarch64-unknown-linux-gnu/stage1-tools-bin/rust-tidy --root-path=/checkout --cargo-path=/checkout/obj/build/aarch64-unknown-linux-gnu/stage0/bin/cargo --output-dir=/checkout/obj/build --concurrency=4 --npm-path=yarn` failed with exit code 1
Created at: src/bootstrap/src/core/build_steps/tool.rs:1612:23
Executed at: src/bootstrap/src/core/build_steps/test.rs:1364:29

Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:00:52
  local time: Sun Feb  1 23:34:10 UTC 2026
  network time: Sun, 01 Feb 2026 23:34:10 GMT
##[error]Process completed with exit code 1.
##[group]Run echo "disk usage:"

cx.type_of(free_alias.def_id).instantiate(cx, free_alias.args).into()
} else {
cx.const_of_item(free_alias.def_id).instantiate(cx, free_alias.args).into()
let ct = cx.const_of_item(free_alias.def_id).instantiate(cx, free_alias.args);
Copy link
Member

Choose a reason for hiding this comment

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

I think we basically don't want this change 🤔 The soundness critical thing for coherence is that normalizing oGCA anon consts is ambiguous. We handle this (incidentally) in normalize_anon_const which will also handle this when normalizing associated constants, not just free consts like we do here.

I would drop this change. I expect this will make diagnostics worse in coherence but I think that's fine for now. Or at least I think we should improve diagnostics of errors involving oGCA anon consts in a different way than complicating soundness critical normalization logic 🤔

Copy link
Member

@BoxyUwU BoxyUwU Feb 3, 2026

Choose a reason for hiding this comment

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

I realise I previously stated:

(less urgent:) don't normalize opaque const items (those with OGCA RHS)

but I disagree with myself now 😆 complicated normalization logic by doing this for diagnostics reasons is pretty meh

&mut self,
goal: Goal<I, ty::NormalizesTo<I>>,
) -> QueryResult<I> {
// FIXME(ogca): should we also stop ogca normalization during coherence here or just in normalize_free_alias?
Copy link
Member

Choose a reason for hiding this comment

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

The existing logic here implicitly gives ambiguity during coherence but also outside of coherence which I think is undesirable. In the failure case we should check if goal.predicate.alias.def_id is an oGCA anon const, and if so return NoSolution if we're outside of coherence, otherwise return AMBIGUOUS

If we don't do this I think we'll get type inference errors whenever the new solver tries to normalize oGCA anon consts 🤔 e.g.

//@ compile-flags: -Znext-solver
#![feature(generic_const_items, min_generic_const_args, opaque_generic_const_args)]

#[type_const]
const FOO<const N: usize>: usize = const { N + 1 };

fn foo() {
    let a: [(); FOO::<1>];
}

probably fails under your PR right now?

Copy link
Member

@BoxyUwU BoxyUwU Feb 5, 2026

Choose a reason for hiding this comment

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

i was wrong ^ the evaluate_const here is not try_evalaute_const from in rustc_trait_selection and implicitly turns the HasInfersOrParams cases into Ok. We need to check for oGCA consts here and return ambiguity explicitly in coherence

@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 3, 2026

@rustbot author

@rustbot rustbot 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 Feb 3, 2026
Comment on lines +605 to +606
// TODO: create tracking issue
(unstable, opaque_generic_const_args, "CURRENT_RUSTC_VERSION", None),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// TODO: create tracking issue
(unstable, opaque_generic_const_args, "CURRENT_RUSTC_VERSION", None),
(unstable, opaque_generic_const_args, "CURRENT_RUSTC_VERSION", Some(151972)),

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 5, 2026

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

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

Labels

A-const-generics Area: const generics (parameters and arguments) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants