-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Move coercion hack from coerce_unsized
to check_cast
#138542
base: master
Are you sure you want to change the base?
Move coercion hack from coerce_unsized
to check_cast
#138542
Conversation
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
… r=<try> Move coercion hack from `coerce_unsized` to `check_cast` r? `@ghost`
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (5883d54): comparison URL. Overall result: ✅ improvements - 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 (primary -2.1%, secondary -2.1%)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 (primary -1.5%, secondary -5.1%)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: 773.519s -> 771.65s (-0.24%) |
Gamer. I like this approach a lot better than #138438 since it's far less invasive, and this should be sufficient to fix the perf regression in #136127 (comment) since it moves the regression totally into the cast check rather than on all coercions. r? lcnr (I am happy to explain the approach more if you have no context, or reassign it if you don't want to review this) |
// cast, so we first check `may_coerce` which also checks that all | ||
// of the nested obligations hold first, *then* only commit to the | ||
// coercion cast if definitely holds. | ||
if fcx.may_coerce(self.expr_ty, self.cast_ty) { |
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.
this feels iffy to me 🤔
both because this is the first use of may_coerce
in the happy path and because the code layout seems a bit odd.
If we were to keep this, please move the may_coerce
into try_coercion_cast
🤔
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.
afaict we call try_coercion_cast
before do_check
to lint trivial casts 🤔
What would break if we just always use do_check
for raw pointers and never attempt to coerce them?
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.
If we were to keep this, please move the
may_coerce
intotry_coercion_cast
We can't do that. That would negatively affect diagnostics in other coercion casts (e.g. for refs) which currently rely on coercion bubbling up a more specific error. We intentionally only want to check may_coerce
when casting only raw pointers, which is the point of this hack in the first place.
The purpose of calling may_coerce
is to check that the nested obligations may hold. This could be written more explicitly, as a probe + coerce + check nested obligations, but it's code duplication.
What would break if we just always use do_check for raw pointers and never attempt to coerce them?
All casts that don't involve identical fat pointees. Like, *mut dyn Trait + 'long
-> *mut dyn Trait + 'short
relies on coercion. We also allow *mut dyn Trait + Send
-> *mut dyn Trait
, and raw pointer upcasting.
In #136127, an
if
statement was added to cause us to not unconditionally commit to an unsize coercion in the case we have a coercion that looks like*const W<dyn Trait> -> *const dyn Trait
, which can't succeed b/c the pointee of the LHS is not sized. Instead, we want to fall through in the cast check into a ptr-to-ptr cast, which equates the metadata of the pointees (in this case, they both havedyn Trait
's metadata).rust/compiler/rustc_hir_typeck/src/coercion.rs
Lines 602 to 651 in 4d30011
However, due to how it was implemented it resulted in quite a dramatic performance hit. In order to mitigate this, this PR moves this hack into the cast check. I also made the
coerce_unsized
code stop emitting errors eagerly and instead bubble up the obligation that failed in theInferOk
of the coercion, which is on its own a good change IMO since it was weird that we could be emitting hard errors even in amay_coerce
call.