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

Fix ICE with unsized type in const pattern #87065

Merged
merged 1 commit into from
Jul 13, 2021

Conversation

FabianWolff
Copy link
Contributor

Fixes #87046. The deref_const() query currently contains the following check:

let ty = match mplace.meta {
MemPlaceMeta::None => mplace.layout.ty,
MemPlaceMeta::Poison => bug!("poison metadata in `deref_const`: {:#?}", mplace),
// In case of unsized types, figure out the real type behind.
MemPlaceMeta::Meta(scalar) => match mplace.layout.ty.kind() {
ty::Str => bug!("there's no sized equivalent of a `str`"),
ty::Slice(elem_ty) => tcx.mk_array(elem_ty, scalar.to_machine_usize(&tcx).unwrap()),
_ => bug!(
"type {} should not have metadata, but had {:?}",
mplace.layout.ty,
mplace.meta
),
},
};

i.e. this will cause an ICE for every unsized type except slices. An error is reported with my changes if such a type is used as a const pattern (this should not be a breaking change, since so far, this has caused an ICE).

@rust-highfive
Copy link
Collaborator

r? @oli-obk

(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 Jul 11, 2021
};
self.behind_reference.set(old);
val
if !pointee_ty.is_sized(tcx.at(span), param_env) {
Copy link
Contributor

Choose a reason for hiding this comment

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

that certainly avoids the ICE, but we could also make such patterns legal. I think we should. @rust-lang/wg-const-eval what do you think about these?

Copy link
Member

Choose a reason for hiding this comment

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

This always does span_err or delay_span_bug... how does this make anything legal?

Copy link
Contributor

@oli-obk oli-obk Jul 11, 2021

Choose a reason for hiding this comment

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

I said "we could ...", I'm fine doing this PR, it changes an ICE to an error, but why should the code in the new test not be permitted? It seems perfectly alright to me

Copy link
Member

Choose a reason for hiding this comment

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

I'm not familiar enough with pattern matching code to comment on this.

But I'd prefer we port that code to valtrees first, then I will be a lot more confident that what that code does makes sense in my mental model of rustc. :)

Copy link

Choose a reason for hiding this comment

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

@oli-obk What would be a good way to direct attention to this? I would find it incredibly useful to be able to use newtypes around str as const patterns.

Copy link
Contributor

Choose a reason for hiding this comment

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

Valtrees are being worked on right now, once we have them, this becomes trivial to do and we can ask T-lang whether they think this should br supported, too

Copy link

Choose a reason for hiding this comment

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

Cool thanks

@oli-obk
Copy link
Contributor

oli-obk commented Jul 12, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Jul 12, 2021

📌 Commit 79f0743 has been approved by oli-obk

@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 Jul 12, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 13, 2021
Rollup of 11 pull requests

Successful merges:

 - rust-lang#86344 (Split MaybeUninit::write into new feature gate and stabilize it)
 - rust-lang#86811 (Remove unstable `io::Cursor::remaining`)
 - rust-lang#86846 (stdio_locked: add tracking issue)
 - rust-lang#86887 (rustdoc: remove dead code in `clean`)
 - rust-lang#87007 (Fix rust-analyzer install when not available.)
 - rust-lang#87035 (Fix implementors display)
 - rust-lang#87065 (Fix ICE with unsized type in const pattern)
 - rust-lang#87070 (Simplify future incompatible reporting.)
 - rust-lang#87077 (:arrow_up: rust-analyzer)
 - rust-lang#87078 (Rustdoc: suggest removing disambiguator if linking to field)
 - rust-lang#87089 (CTFE engine: small cleanups)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 47a4184 into rust-lang:master Jul 13, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jul 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

Strongly-typed str-wrapper const results in ICE in pattern match
7 participants