Skip to content

Commit fba9d14

Browse files
committed
Lower type ascriptions to HAIR and MIR
1 parent 8132d4e commit fba9d14

File tree

7 files changed

+73
-4
lines changed

7 files changed

+73
-4
lines changed

src/librustc_mir/build/expr/as_place.rs

+35
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use build::{BlockAnd, BlockAndExtension, Builder};
1616
use hair::*;
1717
use rustc::mir::interpret::EvalErrorKind::BoundsCheck;
1818
use rustc::mir::*;
19+
use rustc::ty::Variance;
1920

2021
use rustc_data_structures::indexed_vec::Idx;
2122

@@ -136,6 +137,40 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
136137
ty: expr.ty,
137138
}))),
138139

140+
ExprKind::PlaceTypeAscription { source, user_ty } => {
141+
let place = unpack!(block = this.as_place(block, source));
142+
this.cfg.push(
143+
block,
144+
Statement {
145+
source_info,
146+
kind: StatementKind::AscribeUserType(
147+
place.clone(),
148+
Variance::Invariant,
149+
user_ty,
150+
),
151+
},
152+
);
153+
block.and(place)
154+
}
155+
ExprKind::ValueTypeAscription { source, user_ty } => {
156+
let source = this.hir.mirror(source);
157+
let temp = unpack!(
158+
block = this.as_temp(block, source.temp_lifetime, source, mutability)
159+
);
160+
this.cfg.push(
161+
block,
162+
Statement {
163+
source_info,
164+
kind: StatementKind::AscribeUserType(
165+
Place::Local(temp.clone()),
166+
Variance::Invariant,
167+
user_ty,
168+
),
169+
},
170+
);
171+
block.and(Place::Local(temp))
172+
}
173+
139174
ExprKind::Array { .. }
140175
| ExprKind::Tuple { .. }
141176
| ExprKind::Adt { .. }

src/librustc_mir/build/expr/as_rvalue.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
386386
| ExprKind::Continue { .. }
387387
| ExprKind::Return { .. }
388388
| ExprKind::InlineAsm { .. }
389-
| ExprKind::StaticRef { .. } => {
389+
| ExprKind::StaticRef { .. }
390+
| ExprKind::PlaceTypeAscription { .. }
391+
| ExprKind::ValueTypeAscription { .. } => {
390392
// these do not have corresponding `Rvalue` variants,
391393
// so make an operand and then return that
392394
debug_assert!(match Category::of(&expr.kind) {

src/librustc_mir/build/expr/category.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ impl Category {
5050
| ExprKind::Index { .. }
5151
| ExprKind::SelfRef
5252
| ExprKind::VarRef { .. }
53-
| ExprKind::StaticRef { .. } => Some(Category::Place),
53+
| ExprKind::StaticRef { .. }
54+
| ExprKind::PlaceTypeAscription { .. }
55+
| ExprKind::ValueTypeAscription { .. } => Some(Category::Place),
5456

5557
ExprKind::LogicalOp { .. }
5658
| ExprKind::If { .. }

src/librustc_mir/build/expr/into.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
391391
| ExprKind::Adt { .. }
392392
| ExprKind::Closure { .. }
393393
| ExprKind::Literal { .. }
394-
| ExprKind::Yield { .. } => {
394+
| ExprKind::Yield { .. }
395+
| ExprKind::PlaceTypeAscription { .. }
396+
| ExprKind::ValueTypeAscription { .. } => {
395397
debug_assert!(match Category::of(&expr.kind).unwrap() {
396398
Category::Rvalue(RvalueFunc::Into) => false,
397399
_ => true,

src/librustc_mir/hair/cx/expr.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,23 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
718718
ExprKind::Cast { source }
719719
}
720720
}
721-
hir::ExprKind::Type(ref source, _) => return source.make_mirror(cx),
721+
hir::ExprKind::Type(ref source, ref ty) => {
722+
let user_provided_tys = cx.tables.user_provided_tys();
723+
let user_ty = *user_provided_tys
724+
.get(ty.hir_id)
725+
.expect(&format!("{:?} not found in user_provided_tys, source: {:?}", ty, source));
726+
if source.is_place_expr() {
727+
ExprKind::PlaceTypeAscription {
728+
source: source.to_ref(),
729+
user_ty,
730+
}
731+
} else {
732+
ExprKind::ValueTypeAscription {
733+
source: source.to_ref(),
734+
user_ty,
735+
}
736+
}
737+
}
722738
hir::ExprKind::Box(ref value) => {
723739
ExprKind::Box {
724740
value: value.to_ref(),

src/librustc_mir/hair/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ pub enum ExprKind<'tcx> {
268268
fields: Vec<FieldExprRef<'tcx>>,
269269
base: Option<FruInfo<'tcx>>
270270
},
271+
PlaceTypeAscription {
272+
source: ExprRef<'tcx>,
273+
/// Type that the user gave to this expression
274+
user_ty: CanonicalTy<'tcx>,
275+
},
276+
ValueTypeAscription {
277+
source: ExprRef<'tcx>,
278+
/// Type that the user gave to this expression
279+
user_ty: CanonicalTy<'tcx>,
280+
},
271281
Closure {
272282
closure_id: DefId,
273283
substs: UpvarSubsts<'tcx>,

src/librustc_typeck/check/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4118,6 +4118,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
41184118
hir::ExprKind::Type(ref e, ref t) => {
41194119
let ty = self.to_ty(&t);
41204120
self.check_expr_eq_type(&e, ty);
4121+
let c_ty = self.infcx.canonicalize_response(&ty);
4122+
self.tables.borrow_mut().user_provided_tys_mut().insert(t.hir_id, c_ty);
41214123
ty
41224124
}
41234125
hir::ExprKind::Array(ref args) => {

0 commit comments

Comments
 (0)