Skip to content

Commit 1dc6b2a

Browse files
committed
Strip OpaqueCast during RevealAll.
1 parent e373d99 commit 1dc6b2a

File tree

10 files changed

+57
-26
lines changed

10 files changed

+57
-26
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ pub(crate) fn codegen_place<'tcx>(
875875
PlaceElem::Deref => {
876876
cplace = cplace.place_deref(fx);
877877
}
878-
PlaceElem::OpaqueCast(ty) => cplace = cplace.place_opaque_cast(fx, ty),
878+
PlaceElem::OpaqueCast(ty) => bug!("encountered OpaqueCast({ty}) in codegen"),
879879
PlaceElem::Field(field, _ty) => {
880880
cplace = cplace.place_field(fx, field);
881881
}

compiler/rustc_codegen_ssa/src/mir/place.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
463463
mir::ProjectionElem::Field(ref field, _) => {
464464
cg_base.project_field(bx, field.index())
465465
}
466-
mir::ProjectionElem::OpaqueCast(ty) => cg_base.project_type(bx, ty),
466+
mir::ProjectionElem::OpaqueCast(ty) => {
467+
bug!("encountered OpaqueCast({ty}) in codegen")
468+
}
467469
mir::ProjectionElem::Index(index) => {
468470
let index = &mir::Operand::Copy(mir::Place::from(index));
469471
let index = self.codegen_operand(bx, index);

compiler/rustc_const_eval/src/interpret/projection.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ where
316316
{
317317
use rustc_middle::mir::ProjectionElem::*;
318318
Ok(match proj_elem {
319-
OpaqueCast(ty) => base.transmute(self.layout_of(ty)?, self)?,
319+
OpaqueCast(ty) => bug!("OpaqueCast({ty}) encountered after borrowck"),
320320
Field(field, _) => self.project_field(base, field.index())?,
321321
Downcast(_, variant) => self.project_downcast(base, variant)?,
322322
Deref => self.deref_pointer(&base.to_op(self)?)?.into(),

compiler/rustc_mir_transform/src/reveal_all.rs

+19
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ impl<'tcx> MutVisitor<'tcx> for RevealAllVisitor<'tcx> {
2525
self.tcx
2626
}
2727

28+
#[inline]
29+
fn visit_place(
30+
&mut self,
31+
place: &mut Place<'tcx>,
32+
_context: PlaceContext,
33+
_location: Location,
34+
) {
35+
// `OpaqueCast` projections are only needed if there are opaque types on which projections are performed.
36+
// After the `RevealAll` pass, all opaque types are replaced with their hidden types, so we don't need these
37+
// projections anymore.
38+
place.projection = self.tcx.mk_place_elems(
39+
&place
40+
.projection
41+
.into_iter()
42+
.filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(_)))
43+
.collect::<Vec<_>>(),
44+
);
45+
}
46+
2847
#[inline]
2948
fn visit_constant(&mut self, constant: &mut ConstOperand<'tcx>, _: Location) {
3049
// We have to use `try_normalize_erasing_regions` here, since it's

tests/ui/async-await/in-trait/indirect-recursion-issue-112047.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0391]: cycle detected when computing layout of `{async fn body@$DIR/indir
22
|
33
= note: ...which requires computing layout of `<<A as First>::Second as Second>::{opaque#0}`...
44
= note: ...which again requires computing layout of `{async fn body@$DIR/indirect-recursion-issue-112047.rs:35:27: 37:6}`, completing the cycle
5-
= note: cycle used when computing layout of `<impl at $DIR/indirect-recursion-issue-112047.rs:31:1: 31:21>::second::{opaque#0}`
5+
= note: cycle used when computing layout of `{async block@$DIR/indirect-recursion-issue-112047.rs:8:13: 10:6}`
66
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
77

88
error: aborting due to previous error

tests/ui/polymorphization/generators.rs

-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ where
3232

3333
#[rustc_polymorphize_error]
3434
pub fn unused_type<T>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
35-
//~^ ERROR item has unused generic parameters
3635
|| {
3736
//~^ ERROR item has unused generic parameters
3837
yield 1;
@@ -58,7 +57,6 @@ pub fn used_type_in_return<R: Default>() -> impl Generator<(), Yield = u32, Retu
5857

5958
#[rustc_polymorphize_error]
6059
pub fn unused_const<const T: u32>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
61-
//~^ ERROR item has unused generic parameters
6260
|| {
6361
//~^ ERROR item has unused generic parameters
6462
yield 1;

tests/ui/polymorphization/generators.stderr

+3-17
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,20 @@ LL | #![feature(generic_const_exprs, generators, generator_trait, rustc_attrs)]
88
= note: `#[warn(incomplete_features)]` on by default
99

1010
error: item has unused generic parameters
11-
--> $DIR/generators.rs:36:5
11+
--> $DIR/generators.rs:35:5
1212
|
1313
LL | pub fn unused_type<T>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
1414
| - generic parameter `T` is unused
15-
LL |
1615
LL | || {
1716
| ^^
1817

1918
error: item has unused generic parameters
20-
--> $DIR/generators.rs:34:8
21-
|
22-
LL | pub fn unused_type<T>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
23-
| ^^^^^^^^^^^ - generic parameter `T` is unused
24-
25-
error: item has unused generic parameters
26-
--> $DIR/generators.rs:62:5
19+
--> $DIR/generators.rs:60:5
2720
|
2821
LL | pub fn unused_const<const T: u32>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
2922
| ------------ generic parameter `T` is unused
30-
LL |
3123
LL | || {
3224
| ^^
3325

34-
error: item has unused generic parameters
35-
--> $DIR/generators.rs:60:8
36-
|
37-
LL | pub fn unused_const<const T: u32>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
38-
| ^^^^^^^^^^^^ ------------ generic parameter `T` is unused
39-
40-
error: aborting due to 4 previous errors; 1 warning emitted
26+
error: aborting due to 2 previous errors; 1 warning emitted
4127

tests/ui/type-alias-impl-trait/cross_inference_pattern_bug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// compile-flags: --edition=2021
2-
// check-pass
2+
// build-pass
33
#![feature(type_alias_impl_trait)]
44

55
fn main() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// build-pass
2+
// edition: 2021
3+
4+
#![feature(type_alias_impl_trait)]
5+
6+
fn foo<T>(x: T) {
7+
type Opaque<T> = impl Sized;
8+
let foo: Opaque<T> = (x,);
9+
let (a,): (T,) = foo;
10+
}
11+
12+
const fn bar<T: Copy>(x: T) {
13+
type Opaque<T: Copy> = impl Copy;
14+
let foo: Opaque<T> = (x, 2u32);
15+
let (a, b): (T, u32) = foo;
16+
}
17+
18+
fn main() {
19+
foo::<u32>(1);
20+
bar::<u32>(1);
21+
const CONST: () = bar::<u32>(42u32);
22+
CONST
23+
}

tests/ui/type-alias-impl-trait/indirect-recursion-issue-112047.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
error[E0391]: cycle detected when computing layout of `{async block@$DIR/indirect-recursion-issue-112047.rs:23:9: 23:42}`
22
|
3-
= note: ...which requires computing layout of `core::mem::maybe_uninit::MaybeUninit<recur::{opaque#0}>`...
43
= note: ...which requires computing layout of `core::mem::maybe_uninit::MaybeUninit<{async fn body@$DIR/indirect-recursion-issue-112047.rs:15:31: 17:2}>`...
54
= note: ...which requires computing layout of `core::mem::manually_drop::ManuallyDrop<{async fn body@$DIR/indirect-recursion-issue-112047.rs:15:31: 17:2}>`...
65
= note: ...which requires computing layout of `{async fn body@$DIR/indirect-recursion-issue-112047.rs:15:31: 17:2}`...
76
= note: ...which requires computing layout of `core::mem::maybe_uninit::MaybeUninit<<() as Recur>::Recur>`...
87
= note: ...which requires computing layout of `core::mem::maybe_uninit::MaybeUninit<{async block@$DIR/indirect-recursion-issue-112047.rs:23:9: 23:42}>`...
98
= note: ...which requires computing layout of `core::mem::manually_drop::ManuallyDrop<{async block@$DIR/indirect-recursion-issue-112047.rs:23:9: 23:42}>`...
109
= note: ...which again requires computing layout of `{async block@$DIR/indirect-recursion-issue-112047.rs:23:9: 23:42}`, completing the cycle
11-
= note: cycle used when computing layout of `<impl at $DIR/indirect-recursion-issue-112047.rs:19:1: 19:18>::Recur`
10+
note: cycle used when elaborating drops for `<impl at $DIR/indirect-recursion-issue-112047.rs:19:1: 19:18>::recur`
11+
--> $DIR/indirect-recursion-issue-112047.rs:22:5
12+
|
13+
LL | fn recur(self) -> Self::Recur {
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1215
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
1316

1417
error: aborting due to previous error

0 commit comments

Comments
 (0)