Skip to content

Fix ICE with inferred type in const or static item #89161

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 1 commit into from

Conversation

FabianWolff
Copy link
Contributor

Fixes #88643. Currently, there is a check in the compiler that handles the special case of a constant/static variable having a trait object type:

// (#75889): Account for `const C: dyn Fn() -> _ = "";`
if let hir::TyKind::TraitObject(..) = ty.kind {

But this fails to account for the possibility that the dyn type could be nested (as in Vec<dyn Fn(& _)>), which causes the ICE described in #88643.

@rust-highfive
Copy link
Contributor

r? @michaelwoerister

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 21, 2021
@apiraino apiraino added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 22, 2021
@michaelwoerister
Copy link
Member

r? @nikomatsakis maybe?

@bors
Copy link
Collaborator

bors commented Oct 2, 2021

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

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

I'm not overly familiar with this code.

--> $DIR/issue-74086.rs:2:17
|
LL | static BUG: fn(_) -> u8 = |_| 8;
| ^^^^^^^^^^^ not allowed in type signatures
Copy link
Contributor

Choose a reason for hiding this comment

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

this span doesn't seem as helpful as before :(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, though there are now a few almost-duplicate diagnostics if the static or const item has a function pointer type. I haven't been able to track this down/avoid this.

@nikomatsakis
Copy link
Contributor

Sorry, my review comments were ... kind of inscrutable! I was in a hurry. What I wanted to ask was: can you explain what's going wrong? I'm not overly familiar with this code and I can't quite tell what it's intent is.

@FabianWolff
Copy link
Contributor Author

Sure, although I'm no expert on this code either, I just tried to fix the bug in #88643.

Basically, as you can see e.g. in #88643 (comment), the main problem is:

error: internal compiler error: bad_placeholder_type
  --> ice.rs:11:64
   |
11 | static CALLBACKS: HashMap<*const dyn WidgetExt, dyn FnMut(&mut _) + 'static> = HashMap::new();
   |                                                                ^
   |
   = note: delayed at compiler/rustc_typeck/src/collect.rs:390:20

In other words, there should be an error about the use of _ in the type of a static/const item, but there isn't. From my understanding, this error should be emitted around here:

ItemKind::Static(ty, .., body_id) => {
if is_suggestable_infer_ty(ty) {
infer_placeholder_type(
tcx,
def_id,
body_id,
ty.span,
item.ident,
"static variable",
)
} else {
icx.to_ty(ty)
}
}
ItemKind::Const(ty, body_id) => {
if is_suggestable_infer_ty(ty) {
infer_placeholder_type(
tcx, def_id, body_id, ty.span, item.ident, "constant",
)
} else {
icx.to_ty(ty)
}
}

infer_placeholder_type() can emit errors, but apparently it doesn't handle dyn types properly, which is why there already is some special case handling here:

// (#75889): Account for `const C: dyn Fn() -> _ = "";`
if let hir::TyKind::TraitObject(..) = ty.kind {

However, the latter check only handles dyn as the outermost type, but in #88643, the dyn appears as part of a generic argument to HashMap (i.e. ty.kind is not TraitObject), which is not caught by this check.

I have therefore extended this check to not only look at the outermost type kind, but to actually visit the type and see whether it finds a _ somewhere. I have wrapped the check in if !is_suggestable_infer_ty(ty) {} to match the check in type_of.rs to avoid duplicate diagnostics, but apparently a few of them still get through.

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 24, 2021
@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 16, 2021
@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 5, 2021
@pnkfelix
Copy link
Member

@rustbot assign @pnkfelix

@rustbot rustbot assigned pnkfelix and unassigned nikomatsakis Jan 13, 2022
@bors
Copy link
Collaborator

bors commented Jan 21, 2022

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

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 27, 2022
@Dylan-DPC Dylan-DPC 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 Feb 28, 2022
@Dylan-DPC
Copy link
Member

@FabianWolff if you can rebase this, we can push this forward

@JohnCSimon
Copy link
Member

@FabianWolff
Ping from triage: I'm closing this due to inactivity, Please reopen when you are ready to continue with this. Thanks for your contribution.

@rustbot label: +S-inactive

@JohnCSimon JohnCSimon closed this Apr 11, 2022
@rustbot rustbot added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Apr 11, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jun 13, 2022
…yn-obj, r=pnkfelix

Harden bad placeholder checks on statics/consts

Resubmission of rust-lang#89161
Fixes rust-lang#88643

In rust-lang#83739, I added a check for trait objects on statics/consts but it wasn't robust. `is_suggestable_infer_ty` fn does a more strict check and finds more bad placeholders. See rust-lang#89161 (comment) for the more detailed explanation.

r? `@pnkfelix` as you're the reviewer of the previous PR
@Dylan-DPC Dylan-DPC removed the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Jun 19, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

error: internal compiler error: compiler/rustc_passes/src/dead.rs:122:13: no type-dependent def for method
10 participants