-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Add a separate error for dyn Trait
in const fn
#89021
Add a separate error for dyn Trait
in const fn
#89021
Conversation
Previously "trait bounds other than `Sized` on const fn parameters are unstable" error was used for both trait bounds (<T: Trait>) and trait objects (dyn Trait). This was pretty confusing. This patch adds a separeta error for trait objects: "trait objects in const fn are unstable". The error for trait bounds is otherwise intact.
@@ -7,9 +7,9 @@ struct Hide(HasDyn); | |||
const fn no_inner_dyn_trait(_x: Hide) {} | |||
const fn no_inner_dyn_trait2(x: Hide) { | |||
x.0.field; | |||
//~^ ERROR trait bounds other than `Sized` | |||
//~^ ERROR trait objects in const fn are 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.
For the cases of fields like this one it would be nice to have a span_note
pointing at the type and field's type to show how it is a trait object.
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.
It would be really nice! Can we implement this though? It seems like we only check locals and not expressions:
rust/compiler/rustc_const_eval/src/transform/check_consts/check.rs
Lines 245 to 259 in f84000d
for (idx, local) in body.local_decls.iter_enumerated() { | |
// Handle the return place below. | |
if idx == RETURN_PLACE || local.internal { | |
continue; | |
} | |
self.span = local.source_info.span; | |
self.check_local_or_return_ty(local.ty, idx); | |
} | |
// impl trait is gone in MIR, so check the return type of a const fn by its signature | |
// instead of the type of the return place. | |
self.span = body.local_decls[RETURN_PLACE].source_info.span; | |
let return_ty = tcx.fn_sig(def_id).output(); | |
self.check_local_or_return_ty(return_ty.skip_binder(), RETURN_PLACE); |
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 not sure if we can't get that info from local.local_info
or local.source_info
(I'm not that familiar with const eval and have to relearn it every time I look at it).
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.
Hm, after reading docs I can't see how we can get the expression, but maybe I'm missing something
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.
Can you check what the contents of local.local_info
are? I assume it is going to be a VarBindingForm
. If it is, what are the contents of opt_match_place
?
Alternatively, if the data isn't there, it could potentially be added.
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.
So in the case of our interest (field access) local.local_info
is simply None
:(
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.
In that case, we'll have to make more dramatic changes so that we can track that data :-/
It's probably not high priority, but if you desire to take that on, I'd be more than happy to see it through!
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 don't think I'm interested enough in that. Thanks for offering help though!
@bors r+ |
📌 Commit f84000d has been approved by |
…_trait_in_const_fn, r=estebank Add a separate error for `dyn Trait` in `const fn` Previously "trait bounds other than `Sized` on const fn parameters are unstable" error was used for both trait bounds (`<T: Trait>`) and trait objects (`dyn Trait`). This was pretty confusing. This PR adds a separate error for trait objects: "trait objects in const fn are unstable". The error for trait bounds is otherwise intact. This is follow up to rust-lang#88907 r? `@estebank` `@rustbot` label: +A-diagnostics
Rollup of 10 pull requests Successful merges: - rust-lang#87960 (Suggest replacing an inexisting field for an unmentioned field) - rust-lang#88855 (Allow simd_shuffle to accept vectors of any length) - rust-lang#88966 (Check for shadowing issues involving block labels) - rust-lang#88996 (Fix linting when trailing macro expands to a trailing semi) - rust-lang#89017 (fix potential race in AtomicU64 time monotonizer) - rust-lang#89021 (Add a separate error for `dyn Trait` in `const fn`) - rust-lang#89051 (Add intra-doc links and small changes to `std::os` to be more consistent) - rust-lang#89053 (refactor: VecDeques IntoIter fields to private) - rust-lang#89055 (Suggest better place to add call parentheses for method expressions wrapped in parentheses) - rust-lang#89081 (Fix a typo) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Previously "trait bounds other than
Sized
on const fn parameters are unstable" error was used for both trait bounds (<T: Trait>
) and trait objects (dyn Trait
). This was pretty confusing.This PR adds a separate error for trait objects: "trait objects in const fn are unstable". The error for trait bounds is otherwise intact.
This is follow up to #88907
r? @estebank
@rustbot label: +A-diagnostics