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

Use correct substs in enum discriminant cast #98429

Merged
merged 5 commits into from
Jun 25, 2022
Merged
Changes from 1 commit
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
191 changes: 96 additions & 95 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::thir::cx::region::Scope;
use crate::thir::cx::Cx;
use crate::thir::util::UserAnnotatedTyHelpers;
use rustc_data_structures::stack::ensure_sufficient_stack;
Expand Down Expand Up @@ -158,6 +159,100 @@ impl<'tcx> Cx<'tcx> {
Expr { temp_lifetime, ty: adjustment.target, span, kind }
}

fn mirror_expr_cast(
lcnr marked this conversation as resolved.
Show resolved Hide resolved
&mut self,
source: &'tcx hir::Expr<'tcx>,
temp_lifetime: Option<Scope>,
span: Span,
) -> ExprKind<'tcx> {
let tcx = self.tcx;

// Check to see if this cast is a "coercion cast", where the cast is actually done
// using a coercion (or is a no-op).
if self.typeck_results().is_coercion_cast(source.hir_id) {
// Convert the lexpr to a vexpr.
ExprKind::Use { source: self.mirror_expr(source) }
} else if self.typeck_results().expr_ty(source).is_region_ptr() {
// Special cased so that we can type check that the element
// type of the source matches the pointed to type of the
// destination.
ExprKind::Pointer {
source: self.mirror_expr(source),
cast: PointerCast::ArrayToPointer,
}
} else {
// check whether this is casting an enum variant discriminant
// to prevent cycles, we refer to the discriminant initializer
// which is always an integer and thus doesn't need to know the
// enum's layout (or its tag type) to compute it during const eval
// Example:
// enum Foo {
// A,
// B = A as isize + 4,
// }
// The correct solution would be to add symbolic computations to miri,
// so we wouldn't have to compute and store the actual value

let hir::ExprKind::Path(ref qpath) = source.kind else {
return ExprKind::Cast { source: self.mirror_expr(source)};
};

let res = self.typeck_results().qpath_res(qpath, source.hir_id);
let (discr_did, discr_offset, discr_ty, substs) = {
Copy link
Contributor

Choose a reason for hiding this comment

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

why use that let here instead of keeping the original variables in scope

let ty = self.typeck_results().node_type(source.hir_id);
let ty::Adt(adt_def, substs) = ty.kind() else {
return ExprKind::Cast { source: self.mirror_expr(source)};
};
let Res::Def(
DefKind::Ctor(CtorOf::Variant, CtorKind::Const),
variant_ctor_id,
) = res else {
return ExprKind::Cast { source: self.mirror_expr(source)};
};
Copy link
Contributor

@lcnr lcnr Jun 24, 2022

Choose a reason for hiding this comment

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

wrong indentation, rustfmt doesn't work for let else for now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm did call ./x.py fmt this time

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, but rustfmt does not work, you have to manually fix indentation for now


let idx = adt_def.variant_index_with_ctor_id(variant_ctor_id);
let (d, o) = adt_def.discriminant_def_for_variant(idx);
use rustc_middle::ty::util::IntTypeExt;
let ty = adt_def.repr().discr_type();
let ty = ty.to_ty(tcx);
(d, o, ty, substs)
};

let param_env_ty = self.param_env.and(discr_ty);
let size = tcx
.layout_of(param_env_ty)
.unwrap_or_else(|e| {
panic!("could not compute layout for {:?}: {:?}", param_env_ty, e)
})
.size;

let lit = ScalarInt::try_from_uint(discr_offset as u128, size).unwrap();
let kind = ExprKind::NonHirLiteral { lit, user_ty: None };
let offset = self.thir.exprs.push(Expr { temp_lifetime, ty: discr_ty, span, kind });

let source = match discr_did {
Some(did) => {
// in case we are offsetting from a computed discriminant
// and not the beginning of discriminants (which is always `0`)

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Some(did) => {
// in case we are offsetting from a computed discriminant
// and not the beginning of discriminants (which is always `0`)
// in case we are offsetting from a computed discriminant
// and not the beginning of discriminants (which is always `0`)
Some(did) => {

let kind = ExprKind::NamedConst { def_id: did, substs, user_ty: None };
let lhs =
self.thir.exprs.push(Expr { temp_lifetime, ty: discr_ty, span, kind });
let bin = ExprKind::Binary { op: BinOp::Add, lhs, rhs: offset };
self.thir.exprs.push(Expr {
temp_lifetime,
ty: discr_ty,
span: span,
kind: bin,
})
}
None => offset,
};

ExprKind::Cast { source }
}
}

fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx> {
let tcx = self.tcx;
let expr_ty = self.typeck_results().expr_ty(expr);
Expand Down Expand Up @@ -604,101 +699,7 @@ impl<'tcx> Cx<'tcx> {
expr, cast_ty.hir_id, user_ty,
);

// Check to see if this cast is a "coercion cast", where the cast is actually done
// using a coercion (or is a no-op).
let cast = if self.typeck_results().is_coercion_cast(source.hir_id) {
// Convert the lexpr to a vexpr.
ExprKind::Use { source: self.mirror_expr(source) }
} else if self.typeck_results().expr_ty(source).is_region_ptr() {
// Special cased so that we can type check that the element
// type of the source matches the pointed to type of the
// destination.
ExprKind::Pointer {
source: self.mirror_expr(source),
cast: PointerCast::ArrayToPointer,
}
} else {
// check whether this is casting an enum variant discriminant
// to prevent cycles, we refer to the discriminant initializer
// which is always an integer and thus doesn't need to know the
// enum's layout (or its tag type) to compute it during const eval
// Example:
// enum Foo {
// A,
// B = A as isize + 4,
// }
// The correct solution would be to add symbolic computations to miri,
// so we wouldn't have to compute and store the actual value
let var = if let hir::ExprKind::Path(ref qpath) = source.kind {
let res = self.typeck_results().qpath_res(qpath, source.hir_id);
self.typeck_results().node_type(source.hir_id).ty_adt_def().and_then(
|adt_def| match res {
Res::Def(
DefKind::Ctor(CtorOf::Variant, CtorKind::Const),
variant_ctor_id,
) => {
let idx = adt_def.variant_index_with_ctor_id(variant_ctor_id);
let (d, o) = adt_def.discriminant_def_for_variant(idx);
use rustc_middle::ty::util::IntTypeExt;
let ty = adt_def.repr().discr_type();
let ty = ty.to_ty(tcx);
Some((d, o, ty))
}
_ => None,
},
)
} else {
None
};
debug!(?var);
let substs = self.typeck_results.node_substs(source.hir_id);
debug!(?substs);

let source = if let Some((did, offset, var_ty)) = var {
let param_env_ty = self.param_env.and(var_ty);
let size = tcx
.layout_of(param_env_ty)
.unwrap_or_else(|e| {
panic!("could not compute layout for {:?}: {:?}", param_env_ty, e)
})
.size;
let lit = ScalarInt::try_from_uint(offset as u128, size).unwrap();
let kind = ExprKind::NonHirLiteral { lit, user_ty: None };
let offset = self.thir.exprs.push(Expr {
temp_lifetime,
ty: var_ty,
span: expr.span,
kind,
});
match did {
Some(did) => {
// in case we are offsetting from a computed discriminant
// and not the beginning of discriminants (which is always `0`)

let kind =
ExprKind::NamedConst { def_id: did, substs, user_ty: None };
let lhs = self.thir.exprs.push(Expr {
temp_lifetime,
ty: var_ty,
span: expr.span,
kind,
});
let bin = ExprKind::Binary { op: BinOp::Add, lhs, rhs: offset };
self.thir.exprs.push(Expr {
temp_lifetime,
ty: var_ty,
span: expr.span,
kind: bin,
})
}
None => offset,
}
} else {
self.mirror_expr(source)
};

ExprKind::Cast { source: source }
};
let cast = self.mirror_expr_cast(*source, temp_lifetime, expr.span);

if let Some(user_ty) = user_ty {
// NOTE: Creating a new Expr and wrapping a Cast inside of it may be
Expand Down