Skip to content

Commit e23ad83

Browse files
committed
add a user_ty annotation to Constant
1 parent d7d4d7c commit e23ad83

File tree

16 files changed

+65
-9
lines changed

16 files changed

+65
-9
lines changed

src/librustc/ich/impls_mir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ impl_stable_hash_for!(enum mir::NullOp {
528528
SizeOf
529529
});
530530

531-
impl_stable_hash_for!(struct mir::Constant<'tcx> { span, ty, literal });
531+
impl_stable_hash_for!(struct mir::Constant<'tcx> { span, ty, user_ty, literal });
532532

533533
impl_stable_hash_for!(struct mir::Location { block, statement_index });
534534

src/librustc/mir/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,12 +1888,15 @@ pub enum Operand<'tcx> {
18881888
/// This implies that the type of the place must be `Copy`; this is true
18891889
/// by construction during build, but also checked by the MIR type checker.
18901890
Copy(Place<'tcx>),
1891+
18911892
/// Move: The value (including old borrows of it) will not be used again.
18921893
///
18931894
/// Safe for values of all types (modulo future developments towards `?Move`).
18941895
/// Correct usage patterns are enforced by the borrow checker for safe code.
18951896
/// `Copy` may be converted to `Move` to enable "last-use" optimizations.
18961897
Move(Place<'tcx>),
1898+
1899+
/// Synthesizes a constant value.
18971900
Constant(Box<Constant<'tcx>>),
18981901
}
18991902

@@ -1909,6 +1912,9 @@ impl<'tcx> Debug for Operand<'tcx> {
19091912
}
19101913

19111914
impl<'tcx> Operand<'tcx> {
1915+
/// Convenience helper to make a constant that refers to the fn
1916+
/// with given def-id and substs. Since this is used to synthesize
1917+
/// MIR, assumes `user_ty` is None.
19121918
pub fn function_handle<'a>(
19131919
tcx: TyCtxt<'a, 'tcx, 'tcx>,
19141920
def_id: DefId,
@@ -1919,6 +1925,7 @@ impl<'tcx> Operand<'tcx> {
19191925
Operand::Constant(box Constant {
19201926
span,
19211927
ty,
1928+
user_ty: None,
19221929
literal: ty::Const::zero_sized(tcx, ty),
19231930
})
19241931
}
@@ -2207,6 +2214,14 @@ impl<'tcx> Debug for Rvalue<'tcx> {
22072214
pub struct Constant<'tcx> {
22082215
pub span: Span,
22092216
pub ty: Ty<'tcx>,
2217+
2218+
/// Optional user-given type: for something like
2219+
/// `collect::<Vec<_>>`, this would be present and would
2220+
/// indicate that `Vec<_>` was explicitly specified.
2221+
///
2222+
/// Needed for NLL to impose user-given type constraints.
2223+
pub user_ty: Option<CanonicalTy<'tcx>>,
2224+
22102225
pub literal: &'tcx ty::Const<'tcx>,
22112226
}
22122227

@@ -2902,6 +2917,7 @@ impl<'tcx> TypeFoldable<'tcx> for Constant<'tcx> {
29022917
Constant {
29032918
span: self.span.clone(),
29042919
ty: self.ty.fold_with(folder),
2920+
user_ty: self.user_ty.fold_with(folder),
29052921
literal: self.literal.fold_with(folder),
29062922
}
29072923
}

src/librustc/mir/visit.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ macro_rules! make_mir_visitor {
213213
self.super_ty(ty);
214214
}
215215

216+
fn visit_canonical_ty(&mut self, ty: & $($mutability)* CanonicalTy<'tcx>) {
217+
self.super_canonical_ty(ty);
218+
}
219+
216220
fn visit_region(&mut self,
217221
region: & $($mutability)* ty::Region<'tcx>,
218222
_: Location) {
@@ -625,9 +629,10 @@ macro_rules! make_mir_visitor {
625629
}
626630

627631
fn super_user_assert_ty(&mut self,
628-
_c_ty: & $($mutability)* CanonicalTy<'tcx>,
632+
c_ty: & $($mutability)* CanonicalTy<'tcx>,
629633
local: & $($mutability)* Local,
630634
location: Location) {
635+
self.visit_canonical_ty(c_ty);
631636
self.visit_local(local, PlaceContext::Validate, location);
632637
}
633638

@@ -740,11 +745,13 @@ macro_rules! make_mir_visitor {
740745
let Constant {
741746
ref $($mutability)* span,
742747
ref $($mutability)* ty,
748+
ref $($mutability)* user_ty,
743749
ref $($mutability)* literal,
744750
} = *constant;
745751

746752
self.visit_span(span);
747753
self.visit_ty(ty, TyContext::Location(location));
754+
drop(user_ty); // no visit method for this
748755
self.visit_const(literal, location);
749756
}
750757

@@ -764,6 +771,9 @@ macro_rules! make_mir_visitor {
764771
self.visit_source_scope(scope);
765772
}
766773

774+
fn super_canonical_ty(&mut self, _ty: & $($mutability)* CanonicalTy<'tcx>) {
775+
}
776+
767777
fn super_ty(&mut self, _ty: & $($mutability)* Ty<'tcx>) {
768778
}
769779

src/librustc_mir/build/expr/as_constant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3131
match kind {
3232
ExprKind::Scope { region_scope: _, lint_level: _, value } =>
3333
this.as_constant(value),
34-
ExprKind::Literal { literal } =>
35-
Constant { span: span, ty: ty, literal: literal },
34+
ExprKind::Literal { literal, user_ty } =>
35+
Constant { span, ty, user_ty, literal },
3636
_ =>
3737
span_bug!(
3838
span,

src/librustc_mir/build/expr/as_rvalue.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
239239
operands.push(Operand::Constant(box Constant {
240240
span: expr_span,
241241
ty: this.hir.tcx().types.u32,
242+
user_ty: None,
242243
literal: ty::Const::from_bits(
243244
this.hir.tcx(),
244245
0,

src/librustc_mir/build/expr/into.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
128128
Constant {
129129
span: expr_span,
130130
ty: this.hir.bool_ty(),
131+
user_ty: None,
131132
literal: this.hir.true_literal(),
132133
});
133134

@@ -136,6 +137,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
136137
Constant {
137138
span: expr_span,
138139
ty: this.hir.bool_ty(),
140+
user_ty: None,
139141
literal: this.hir.false_literal(),
140142
});
141143

src/librustc_mir/build/matches/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
344344
func: Operand::Constant(box Constant {
345345
span: test.span,
346346
ty: mty,
347-
literal: method
347+
user_ty: None, // FIXME
348+
literal: method,
348349
}),
349350
args: vec![val, expect],
350351
destination: Some((eq_result.clone(), eq_block)),

src/librustc_mir/build/misc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3232
place
3333
}
3434

35+
/// Convenience function for creating a literal operand, one
36+
/// without any user type annotation.
3537
pub fn literal_operand(&mut self,
3638
span: Span,
3739
ty: Ty<'tcx>,
@@ -40,6 +42,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
4042
let constant = box Constant {
4143
span,
4244
ty,
45+
user_ty: None,
4346
literal,
4447
};
4548
Operand::Constant(constant)
@@ -69,6 +72,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
6972
Constant {
7073
span: source_info.span,
7174
ty: self.hir.usize_ty(),
75+
user_ty: None,
7276
literal: self.hir.usize_literal(value),
7377
});
7478
temp

src/librustc_mir/hair/cx/expr.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
317317

318318
hir::ExprKind::Lit(ref lit) => ExprKind::Literal {
319319
literal: cx.const_eval_literal(&lit.node, expr_ty, lit.span, false),
320+
user_ty: None,
320321
},
321322

322323
hir::ExprKind::Binary(op, ref lhs, ref rhs) => {
@@ -406,6 +407,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
406407
if let hir::ExprKind::Lit(ref lit) = arg.node {
407408
ExprKind::Literal {
408409
literal: cx.const_eval_literal(&lit.node, expr_ty, lit.span, true),
410+
user_ty: None,
409411
}
410412
} else {
411413
ExprKind::Unary {
@@ -631,7 +633,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
631633
temp_lifetime,
632634
ty,
633635
span: expr.span,
634-
kind: ExprKind::Literal { literal },
636+
kind: ExprKind::Literal { literal, user_ty: None },
635637
}.to_ref();
636638
let offset = mk_const(ty::Const::from_bits(
637639
cx.tcx,
@@ -703,6 +705,7 @@ fn method_callee<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
703705
span: expr.span,
704706
kind: ExprKind::Literal {
705707
literal: ty::Const::zero_sized(cx.tcx(), ty),
708+
user_ty: None, // TODO
706709
},
707710
}
708711
}
@@ -758,6 +761,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
758761
cx.tcx,
759762
cx.tables().node_id_to_type(expr.hir_id),
760763
),
764+
user_ty: None, // TODO
761765
},
762766

763767
Def::Const(def_id) |
@@ -768,6 +772,7 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
768772
substs,
769773
cx.tables().node_id_to_type(expr.hir_id),
770774
),
775+
user_ty: None, // TODO?
771776
},
772777

773778
Def::StructCtor(def_id, CtorKind::Const) |

src/librustc_mir/hair/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc::mir::{BinOp, BorrowKind, Field, UnOp};
1818
use rustc::hir::def_id::DefId;
1919
use rustc::middle::region;
2020
use rustc::ty::subst::Substs;
21-
use rustc::ty::{AdtDef, UpvarSubsts, Region, Ty, Const};
21+
use rustc::ty::{AdtDef, CanonicalTy, UpvarSubsts, Region, Ty, Const};
2222
use rustc::hir;
2323
use syntax::ast;
2424
use syntax_pos::Span;
@@ -272,6 +272,13 @@ pub enum ExprKind<'tcx> {
272272
},
273273
Literal {
274274
literal: &'tcx Const<'tcx>,
275+
276+
/// Optional user-given type: for something like
277+
/// `collect::<Vec<_>>`, this would be present and would
278+
/// indicate that `Vec<_>` was explicitly specified.
279+
///
280+
/// Needed for NLL to impose user-given type constraints.
281+
user_ty: Option<CanonicalTy<'tcx>>,
275282
},
276283
InlineAsm {
277284
asm: &'tcx hir::InlineAsm,

0 commit comments

Comments
 (0)