Skip to content

Introduce const Trait (always-const trait bounds) - #119099

Merged
bors merged 1 commit into
rust-lang:masterfrom
fmease:always-const-trait-bounds
Dec 27, 2023
Merged

Introduce const Trait (always-const trait bounds)#119099
bors merged 1 commit into
rust-lang:masterfrom
fmease:always-const-trait-bounds

Conversation

@fmease

@fmease fmease commented Dec 18, 2023

Copy link
Copy Markdown
Member

Feature const_trait_impl currently lacks a way to express “always const” trait bounds. This makes it impossible to define generic items like fns or structs which contain types that depend on const method calls (*). While the final design and esp. the syntax of effects / keyword generics isn't set in stone, some version of “always const” trait bounds will very likely form a part of it. Further, their implementation is trivial thanks to the effects backbone.

Not sure if this needs t-lang sign-off though.

(*):

#![feature(const_trait_impl, effects, generic_const_exprs)]

fn compute<T: const Trait>() -> Type<{ T::generate() }> { /*…*/ }

struct Store<T: const Trait>
where
    Type<{ T::generate() }>:,
{
    field: Type<{ T::generate() }>,
}

Lastly, “always const” trait bounds are a perfect fit for generic_const_items.

#![feature(const_trait_impl, effects, generic_const_items)]

const DEFAULT<T: const Default>: T = T::default();

Previously, we (oli, fee1-dead and I) wanted to reinterpret ~const Trait as const Trait in generic const items which would've been quite surprising and not very generalizable.
Supersedes #117530.


cc @oli-obk

As discussed
r? fee1-dead (or compiler)

@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 Dec 18, 2023
@fmease fmease added F-const_trait_impl `#![feature(const_trait_impl)]` F-effects `#![feature(effects)]` labels Dec 18, 2023
@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_ast/src/ast.rs Outdated
Comment on lines +2618 to +2611
let id = lcx.next_node_id();
let hir_id = lcx.next_id();

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.

I've moved this {Node,Hir}Id creation below the HirId registration for the hir::ExprKind::Path above to accommodate the control flow, I hope that doesn't break any invariants (?).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No, as long as you didn't move it outside of a nested self.with_* function.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually... hm... it's probably fine...

Comment thread compiler/rustc_ast_passes/src/ast_validation.rs Outdated
Comment thread compiler/rustc_middle/src/ty/mod.rs

@compiler-errors compiler-errors left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we have tests to check that we deny ~const in const items and structs and stuff?

@fmease

fmease commented Dec 18, 2023

Copy link
Copy Markdown
Member Author

Do we have tests to check that we deny ~const in const items and structs and stuff?

Yes, we do: https://github.com/rust-lang/rust/blob/master/tests/ui/rfcs/rfc-2632-const-trait-impl/tilde-const-invalid-places.rs

@fmease
fmease force-pushed the always-const-trait-bounds branch from b265a78 to 1ab36f6 Compare December 19, 2023 11:56
@rustbot rustbot added the A-rustdoc-json Area: Rustdoc JSON backend label Dec 19, 2023
@fmease
fmease marked this pull request as ready for review December 19, 2023 11:57
@fmease fmease removed the A-rustdoc-json Area: Rustdoc JSON backend label Dec 19, 2023
|| self.check(&token::Not)
|| self.check(&token::Question)
|| self.check(&token::Tilde)
|| self.check_keyword(kw::Const)

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.

I need to investigate if this visibly changes the MBE matching behavior for stable users. I hope not since this check is required for correctness.

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.

Doesn't seem to be a problem. I've added a test to ensure that const Trait doesn't regress stable code: tests/ui/rfcs/rfc-2632-const-trait-impl/mbe-bare-trait-objects-const-trait-bounds.rs

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.

It's obvious in hindsight but this doesn't regress the aforementioned test because I haven't (and I won't) add kw::Const & TokenKind::Tilde to can_begin_type.

@rust-log-analyzer

This comment has been minimized.

@fmease
fmease force-pushed the always-const-trait-bounds branch from 1ab36f6 to 932c309 Compare December 19, 2023 13:20
@rustbot rustbot added the A-rustdoc-json Area: Rustdoc JSON backend label Dec 19, 2023
@rustbot

rustbot commented Dec 19, 2023

Copy link
Copy Markdown
Collaborator

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@rust-log-analyzer

This comment has been minimized.

@fmease
fmease force-pushed the always-const-trait-bounds branch 4 times, most recently from 310df46 to 2abd167 Compare December 19, 2023 19:52
@bors

bors commented Dec 22, 2023

Copy link
Copy Markdown
Collaborator

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

Comment thread compiler/rustc_ast/src/ast.rs Outdated
Comment thread compiler/rustc_ast/src/token.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/lib.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/lib.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/lib.rs Outdated
Comment thread compiler/rustc_ast_lowering/src/item.rs Outdated
Comment thread compiler/rustc_hir/src/hir.rs
Comment thread compiler/rustc_hir_analysis/messages.ftl Outdated
Comment thread src/librustdoc/html/format.rs Outdated
Comment thread compiler/rustc_parse/src/parser/ty.rs Outdated
Comment on lines +847 to +851
self.sess.emit_err(errors::ModifierLifetime {
span,
modifier: "const",
padding: " ",
});

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.

I did not see an ui test for the padding suggestion. Could you give me a link to it or add it?

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.

Well, padding didn't actually make sense. I've removed it. I could run-rustfix for the -Zparse-only test tests/ui/parser/bounds-type.rs but I'm not sure if it's worth it esp. since it concerns a maybe-incorrect suggestion.

ghost Dec 24, 2023

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.

No need for run-rustfix, just an ui test that would have covered this code path with stderr would be fine. Though removing it also works.

ghost Dec 24, 2023

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.

It's a tool-only suggestion atm, hence it doesn't show up on stderr.

@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 Dec 22, 2023
@fmease

ghost commented Dec 27, 2023

Copy link
Copy Markdown
Member Author

Network error
@bors retry

@bors bors 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 Dec 27, 2023
@bors

ghost commented Dec 27, 2023

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 3eb48a3 with merge 88d69b7...

@bors

ghost commented Dec 27, 2023

Copy link
Copy Markdown
Collaborator

☀️ Test successful - checks-actions
Approved by: fee1-dead
Pushing 88d69b7 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 27, 2023
@bors
bors merged commit 88d69b7 into rust-lang:master Dec 27, 2023
@rustbot rustbot added this to the 1.77.0 milestone Dec 27, 2023
@fmease
fmease deleted the always-const-trait-bounds branch December 27, 2023 21:33
@rust-timer

ghost commented Dec 27, 2023

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (88d69b7): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

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

Max RSS (memory usage)

Results

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.3% [0.1%, 0.5%] 2
Regressions ❌
(secondary)
0.9% [0.9%, 0.9%] 2
Improvements ✅
(primary)
-0.8% [-0.8%, -0.8%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.0% [-0.8%, 0.5%] 3

Cycles

Results

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.6% [0.5%, 0.8%] 3
Regressions ❌
(secondary)
1.1% [1.1%, 1.1%] 1
Improvements ✅
(primary)
-0.5% [-0.5%, -0.5%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.3% [-0.5%, 0.8%] 4

Binary size

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

Bootstrap: 670.98s -> 672.366s (0.21%)
Artifact size: 312.28 MiB -> 312.33 MiB (0.02%)

@fmease fmease removed the A-rustdoc-json Area: Rustdoc JSON backend label Jan 7, 2024
@ytmimi

ghost commented Jan 21, 2024

Copy link
Copy Markdown
Contributor

@fmease @compiler-errors @fee1-dead I'm hoping I can get some help. I've bisected rust-lang/rustfmt#6035 back to this PR.

With the following input: m!(const N: usize = 0;);

rustfmt 1.7.0-dev (88d69b7 2023-12-27) outputs:

m!(const N: usize = 0;);

but the commit just before (rustfmt 1.7.0-dev (a861c89 2023-12-27)) outputs:

m!(
    const N: usize = 0;
);

Any idea why the formatting would have changed?

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

Labels

F-const_trait_impl `#![feature(const_trait_impl)]` F-effects `#![feature(effects)]` merged-by-bors This PR was explicitly merged by bors. PG-const-traits Project group: Const traits 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.

Projects

Development

Successfully merging this pull request may close these issues.

8 participants