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 region issues when using inline const block #88018

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion compiler/rustc_middle/src/ty/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,26 @@ impl<'tcx> Const<'tcx> {
Self::from_opt_const_arg_anon_const(tcx, ty::WithOptConstParam::unknown(def_id))
}

/// Similar to `from_anon_const`, but with regions erased (suitable for MIR).
pub fn from_anon_const_erased(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx Self {
Self::from_opt_const_arg_anon_const_maybe_erase(
tcx,
ty::WithOptConstParam::unknown(def_id),
true,
)
}

pub fn from_opt_const_arg_anon_const(
tcx: TyCtxt<'tcx>,
def: ty::WithOptConstParam<LocalDefId>,
) -> &'tcx Self {
Self::from_opt_const_arg_anon_const_maybe_erase(tcx, def, false)
}

fn from_opt_const_arg_anon_const_maybe_erase(
tcx: TyCtxt<'tcx>,
def: ty::WithOptConstParam<LocalDefId>,
erase: bool,
) -> &'tcx Self {
debug!("Const::from_anon_const(def={:?})", def);

Expand All @@ -53,7 +70,10 @@ impl<'tcx> Const<'tcx> {

let expr = &tcx.hir().body(body_id).value;

let ty = tcx.type_of(def.def_id_for_type_of());
let mut ty = tcx.type_of(def.def_id_for_type_of());
if erase {
ty = tcx.erase_regions_ty(ty);
}

let lit_input = match expr.kind {
hir::ExprKind::Lit(ref lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: false }),
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,8 @@ impl<'tcx> Cx<'tcx> {
hir::InlineAsmOperand::Const { ref anon_const } => {
let anon_const_def_id =
self.tcx.hir().local_def_id(anon_const.hir_id);
let value = ty::Const::from_anon_const(self.tcx, anon_const_def_id);
let value =
ty::Const::from_anon_const_erased(self.tcx, anon_const_def_id);
let span = self.tcx.hir().span(anon_const.hir_id);

InlineAsmOperand::Const { value, span }
Expand Down Expand Up @@ -563,14 +564,14 @@ impl<'tcx> Cx<'tcx> {

hir::ExprKind::ConstBlock(ref anon_const) => {
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
let value = ty::Const::from_anon_const(self.tcx, anon_const_def_id);
let value = ty::Const::from_anon_const_erased(self.tcx, anon_const_def_id);

ExprKind::ConstBlock { value }
}
// Now comes the rote stuff:
hir::ExprKind::Repeat(ref v, ref count) => {
let count_def_id = self.tcx.hir().local_def_id(count.hir_id);
let count = ty::Const::from_anon_const(self.tcx, count_def_id);
let count = ty::Const::from_anon_const_erased(self.tcx, count_def_id);

ExprKind::Repeat { value: self.mirror_expr(v), count }
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/thir/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
let (lit, neg) = match expr.kind {
hir::ExprKind::ConstBlock(ref anon_const) => {
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
let value = ty::Const::from_anon_const(self.tcx, anon_const_def_id);
let value = ty::Const::from_anon_const_erased(self.tcx, anon_const_def_id);
return *self.const_to_pat(value, expr.hir_id, expr.span, false).kind;
}
hir::ExprKind::Lit(ref lit) => (lit, false),
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_typeck/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
}

let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id));
match parent_node {
let ty = match parent_node {
Node::Ty(&Ty { kind: TyKind::Array(_, ref constant), .. })
| Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
if constant.hir_id == hir_id =>
Expand Down Expand Up @@ -497,7 +497,13 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
DUMMY_SP,
&format!("unexpected const parent in type_of(): {:?}", x),
),
}
};

// Typeck doesn't expect erased regions to be returned from `type_of`.
tcx.fold_regions(ty, &mut false, |r, _| match r {
ty::ReErased => tcx.lifetimes.re_static,
_ => r,
})
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem right... once we erased regions, we shouldn't un-erase them. This can go wrong way too easily.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code above gets the type from tcx.typeck(def_id).node_type(hir_id). The type retrieved has their lifetimes erased by rustc_typeck::check::writeback already.

}

Node::GenericParam(param) => match &param.kind {
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/inline-const/const-expr-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// run-pass

#![allow(incomplete_features)]
#![feature(inline_const)]

// rust-lang/rust#78174: ICE: "cannot convert ReErased to a region vid"

fn main() {
let foo = const { "foo" };
assert_eq!(foo, "foo");
}
13 changes: 13 additions & 0 deletions src/test/ui/inline-const/const-match-pat-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// run-pass

#![allow(incomplete_features)]
#![feature(inline_const)]

// rust-lang/rust#78174: ICE: "cannot convert ReErased to a region vid"

fn main() {
match "foo" {
const { concat!("fo", "o") } => (),
_ => unreachable!(),
}
}