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

Add a separate error for dyn Trait in const fn #89021

Merged

Conversation

WaffleLapkin
Copy link
Member

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

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.
@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 16, 2021
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 16, 2021
@@ -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
Copy link
Contributor

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.

Copy link
Member Author

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:

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);

Copy link
Contributor

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).

Copy link
Member Author

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

Copy link
Contributor

@estebank estebank Sep 18, 2021

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.

Copy link
Member Author

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 :(

Copy link
Contributor

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!

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 don't think I'm interested enough in that. Thanks for offering help though!

@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Sep 17, 2021

📌 Commit f84000d has been approved by estebank

@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 Sep 17, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Sep 19, 2021
…_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
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 19, 2021
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
@bors bors merged commit 61bfe36 into rust-lang:master Sep 19, 2021
@rustbot rustbot added this to the 1.57.0 milestone Sep 19, 2021
@WaffleLapkin WaffleLapkin deleted the separate_error_for_dyn_trait_in_const_fn branch September 19, 2021 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants