-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Conversation
r? fee1-dead |
error[E0277]: the trait bound `cross_crate::NonConst: ~const cross_crate::MyTrait` is not satisfied | ||
--> $DIR/cross-crate.rs:19:5 | ||
| | ||
LL | NonConst.func(); | ||
| ^^^^^^^^^^^^^^^ | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
☔ The latest upstream changes (presumably #132349) made this pull request unmergeable. Please resolve the merge conflicts. |
964af99
to
58614fa
Compare
@@ -1,3 +1,15 @@ | |||
error[E0015]: cannot call non-const fn `<cross_crate::NonConst as cross_crate::MyTrait>::func` in constant functions |
There was a problem hiding this comment.
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.
58614fa
to
579cef2
Compare
} else if tcx.features().const_trait_impl() { | ||
infcx.err_ctxt().report_fulfillment_errors(errors); | ||
} else { | ||
self.check_op(ops::FnCallNonConst { |
There was a problem hiding this comment.
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.
00892c0
to
314b383
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Let's test perf @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
r=me if perf shows nothing significant |
…=<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.
This comment has been minimized.
This comment has been minimized.
💔 Test failed - checks-actions |
AH, yes, rebase... |
314b383
to
505e083
Compare
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
…=<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.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (95d3a79): comparison URL. Overall result: ❌ regressions - no action neededBenchmarking 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 Instruction countThis 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.
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.
CyclesResults (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.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 784.059s -> 784.63s (0.07%) |
@bors r=fee1-dead |
☔ The latest upstream changes (presumably #132435) made this pull request unmergeable. Please resolve the merge conflicts. |
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.
505e083
to
57f2e12
Compare
@bors r=fee1-dead |
☀️ Test successful - checks-actions |
Finished benchmarking commit (705cfe0): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis 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.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 780.79s -> 780.514s (-0.04%) |
// 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() { |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. :(
There was a problem hiding this comment.
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.
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 theeffects
andconst_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.