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

Double-check conditional constness in MIR #132276

Merged
merged 2 commits into from
Nov 1, 2024

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Oct 28, 2024

To prevent any unchecked ~const bounds from leaking through during MIR lowering.

If this check fails, it will eventually just delay a bug, but for now it reports errors. That error reporting may be redundant if we're calling it from code that already doesn't allow ~const (i.e. when the effects and const_trait_impl gates are disabled), but I don't think it's that big of a deal.

edit: This also makes sure that we issue a const stability error if we encounter any function with const conditions when const_trait_impl is not enabled. This ensures that that feature remains airtight.

@rustbot
Copy link
Collaborator

rustbot commented Oct 28, 2024

r? @lcnr

rustbot has assigned @lcnr.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 28, 2024
@compiler-errors
Copy link
Member Author

r? fee1-dead

@rustbot rustbot assigned fee1-dead and unassigned lcnr Oct 28, 2024
Comment on lines 1 to 12
error[E0277]: the trait bound `cross_crate::NonConst: ~const cross_crate::MyTrait` is not satisfied
--> $DIR/cross-crate.rs:19:5
|
LL | NonConst.func();
| ^^^^^^^^^^^^^^^

Copy link
Member

Choose a reason for hiding this comment

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

note that when we constify libcore in the future, this change may be leaking out unstable details (the ~const syntax) on stable code

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll make this fall back to a regular "cannot call in const contexts" error if the const_trait_impl feature gate is not enabled.

@bors
Copy link
Contributor

bors commented Oct 30, 2024

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

@@ -1,3 +1,15 @@
error[E0015]: cannot call non-const fn `<cross_crate::NonConst as cross_crate::MyTrait>::func` in constant functions
Copy link
Member Author

Choose a reason for hiding this comment

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

Duplicated error is NBD imo.

} else if tcx.features().const_trait_impl() {
infcx.err_ctxt().report_fulfillment_errors(errors);
} else {
self.check_op(ops::FnCallNonConst {
Copy link
Member Author

@compiler-errors compiler-errors Oct 30, 2024

Choose a reason for hiding this comment

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

I'm actually leaning towards a different choice here.

If const_trait_impl is not enabled, instead of selecting these const conditions and erroring if they fail, we should probably just reject calling any functions with nontrivial const conditions. I kinda don't want to expose calling, e.g., free functions with ~const bounds on stable.

@compiler-errors compiler-errors force-pushed the enforce-fx-in-mir branch 2 times, most recently from 00892c0 to 314b383 Compare October 30, 2024 18:49
Copy link
Member

@fee1-dead fee1-dead left a comment

Choose a reason for hiding this comment

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

lgtm

@compiler-errors
Copy link
Member Author

Let's test perf

@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 Oct 31, 2024
@fee1-dead
Copy link
Member

r=me if perf shows nothing significant

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 31, 2024
…=<try>

Double-check conditional constness in MIR

To prevent any unchecked `~const` bounds from leaking through during MIR lowering.

If this check fails, it will eventually just delay a bug, but for now it reports errors. That error reporting may be redundant if we're calling it from code that already doesn't allow `~const` (i.e. when the `effects` and `const_trait_impl` gates are disabled), but I don't think it's that big of a deal.

edit: This also makes sure that we issue a const stability error if we encounter *any* function with const conditions when `const_trait_impl` is not enabled. This ensures that that feature remains airtight.
@bors
Copy link
Contributor

bors commented Oct 31, 2024

⌛ Trying commit 314b383 with merge 7e97174...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Oct 31, 2024

💔 Test failed - checks-actions

@bors bors 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 Oct 31, 2024
@compiler-errors
Copy link
Member Author

AH, yes, rebase...

@compiler-errors
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Oct 31, 2024

⌛ Trying commit 505e083 with merge 95d3a79...

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 31, 2024
…=<try>

Double-check conditional constness in MIR

To prevent any unchecked `~const` bounds from leaking through during MIR lowering.

If this check fails, it will eventually just delay a bug, but for now it reports errors. That error reporting may be redundant if we're calling it from code that already doesn't allow `~const` (i.e. when the `effects` and `const_trait_impl` gates are disabled), but I don't think it's that big of a deal.

edit: This also makes sure that we issue a const stability error if we encounter *any* function with const conditions when `const_trait_impl` is not enabled. This ensures that that feature remains airtight.
@bors
Copy link
Contributor

bors commented Oct 31, 2024

☀️ Try build successful - checks-actions
Build commit: 95d3a79 (95d3a7902bae7929446953c1dca12f5e2465aa63)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (95d3a79): comparison URL.

Overall result: ❌ regressions - 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)
1.0% [0.8%, 1.3%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary 2.2%)

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)
2.2% [2.2%, 2.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results (secondary -5.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)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-5.3% [-7.1%, -3.5%] 2
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 784.059s -> 784.63s (0.07%)
Artifact size: 333.59 MiB -> 333.57 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 31, 2024
@compiler-errors
Copy link
Member Author

@bors r=fee1-dead

@bors
Copy link
Contributor

bors commented Oct 31, 2024

📌 Commit 505e083 has been approved by fee1-dead

It is now in the queue for this repository.

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 31, 2024
@bors
Copy link
Contributor

bors commented Nov 1, 2024

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

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 1, 2024
To prevent any conditional constness from leaking through during MIR lowering
…check unless const_trait_impl is enabled

This will help us make sure that we never leak any conditionally const
functions into stable.
@compiler-errors
Copy link
Member Author

@bors r=fee1-dead

@bors
Copy link
Contributor

bors commented Nov 1, 2024

📌 Commit 57f2e12 has been approved by fee1-dead

It is now in the queue for this repository.

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 1, 2024
@bors
Copy link
Contributor

bors commented Nov 1, 2024

⌛ Testing commit 57f2e12 with merge 705cfe0...

@bors
Copy link
Contributor

bors commented Nov 1, 2024

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

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 1, 2024
@bors bors merged commit 705cfe0 into rust-lang:master Nov 1, 2024
7 checks passed
@rustbot rustbot added this to the 1.84.0 milestone Nov 1, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (705cfe0): 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 (secondary 1.0%)

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.1% [2.3%, 4.0%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.3% [-3.3%, -3.3%] 1
All ❌✅ (primary) - - 0

Cycles

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

Binary size

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

Bootstrap: 780.79s -> 780.514s (-0.04%)
Artifact size: 334.96 MiB -> 334.93 MiB (-0.01%)

// If there are any const conditions on this fn and `const_trait_impl`
// is not enabled, simply bail. We shouldn't be able to call conditionally
// const functions on stable.
if !const_conditions.is_empty() && !tcx.features().const_trait_impl() {
Copy link
Member

Choose a reason for hiding this comment

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

You should never call tcx.features() in this code, as that won't properly check for rustc_allow_const_fn_unstable.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, the user still needs to enable the feature gate globally so that we can properly enforce the feature's validity earlier in the compiler.

if !errors.is_empty() {
infcx.err_ctxt().report_fulfillment_errors(errors);
}
self.revalidate_conditional_constness(callee, fn_args, call_source, *fn_span);
Copy link
Member

Choose a reason for hiding this comment

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

I am confused, why is this called outside the if let Some(trait_did) = tcx.trait_of_item(callee) {? Can there be non-trait conditional const calls? Given that the const_trait_impl feature gate is used for these calls, I assume the answer is no...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes? We can put ~const bounds on free const functions.

Copy link
Member Author

@compiler-errors compiler-errors Nov 9, 2024

Choose a reason for hiding this comment

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

Given that the const_trait_impl feature gate is used for these calls

I think you're getting too stuck on the naming of the const_trait_impl feature gate.

The feature gate enables the declaration of const traits, impl const, and the use of ~const trait bounds on conditionally const items, which includes free const function. This is how the gate has been used for the last like... 3 years or whatever.

I don't want to rename the feature gate, since that basically is just churn. the feature gate could be called foo_bar for all it matters.

Copy link
Member

Choose a reason for hiding this comment

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

That's a poorly chosen name. :(

Copy link
Member Author

Choose a reason for hiding this comment

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

🤷 I'm sorry, but I didn't choose it, and I'm not really interested in doing a rename here. That can be done later, and ideally not when there are like 3 other PRs in flight concerning it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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
None yet
Development

Successfully merging this pull request may close these issues.

8 participants