Skip to content

Commit ce6ea47

Browse files
committed
refactor infer function
There was no span available in the cast function, but we need to infer the `x` in `x as char` to `u8`. The spans are now removed from all functions using `infer` and instead added in `eval_const_expr_partial`
1 parent f080b13 commit ce6ea47

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/librustc_const_eval/eval.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,10 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &TyCtxt<'tcx>,
814814
debug!("const call({:?})", call_args);
815815
eval_const_expr_partial(tcx, &result, ty_hint, Some(&call_args))?
816816
},
817-
hir::ExprLit(ref lit) => lit_to_const(&lit.node, tcx, ety, lit.span)?,
817+
hir::ExprLit(ref lit) => match lit_to_const(&lit.node, tcx, ety, lit.span) {
818+
Ok(val) => val,
819+
Err(err) => signal!(e, err),
820+
},
818821
hir::ExprBlock(ref block) => {
819822
match block.expr {
820823
Some(ref expr) => eval_const_expr_partial(tcx, &expr, ty_hint, fn_args)?,
@@ -920,7 +923,10 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &TyCtxt<'tcx>,
920923
};
921924

922925
match (ety.map(|t| &t.sty), result) {
923-
(Some(ref ty_hint), Integral(i)) => Ok(Integral(infer(i, tcx, ty_hint, e.span)?)),
926+
(Some(ref ty_hint), Integral(i)) => match infer(i, tcx, ty_hint) {
927+
Ok(inferred) => Ok(Integral(inferred)),
928+
Err(err) => signal!(e, err),
929+
},
924930
(_, result) => Ok(result),
925931
}
926932
}
@@ -929,15 +935,9 @@ fn infer<'tcx>(
929935
i: ConstInt,
930936
tcx: &TyCtxt<'tcx>,
931937
ty_hint: &ty::TypeVariants<'tcx>,
932-
span: Span
933-
) -> Result<ConstInt, ConstEvalErr> {
938+
) -> Result<ConstInt, ErrKind> {
934939
use syntax::ast::*;
935940

936-
let err = |e| ConstEvalErr {
937-
span: span,
938-
kind: e,
939-
};
940-
941941
match (ty_hint, i) {
942942
(&ty::TyInt(IntTy::I8), result @ I8(_)) => Ok(result),
943943
(&ty::TyInt(IntTy::I16), result @ I16(_)) => Ok(result),
@@ -983,17 +983,17 @@ fn infer<'tcx>(
983983
Err(_) => Ok(Usize(ConstUsize::Us32(i as u32))),
984984
}
985985
},
986-
(&ty::TyUint(_), InferSigned(_)) => Err(err(IntermediateUnsignedNegative)),
986+
(&ty::TyUint(_), InferSigned(_)) => Err(IntermediateUnsignedNegative),
987987

988-
(&ty::TyInt(ity), i) => Err(err(TypeMismatch(ity.to_string(), i))),
989-
(&ty::TyUint(ity), i) => Err(err(TypeMismatch(ity.to_string(), i))),
988+
(&ty::TyInt(ity), i) => Err(TypeMismatch(ity.to_string(), i)),
989+
(&ty::TyUint(ity), i) => Err(TypeMismatch(ity.to_string(), i)),
990990

991991
(&ty::TyEnum(ref adt, _), i) => {
992992
let hints = tcx.lookup_repr_hints(adt.did);
993993
let int_ty = tcx.enum_repr_type(hints.iter().next());
994-
infer(i, tcx, &int_ty.to_ty(tcx).sty, span)
994+
infer(i, tcx, &int_ty.to_ty(tcx).sty)
995995
},
996-
(_, i) => Err(err(BadType(ConstVal::Integral(i)))),
996+
(_, i) => Err(BadType(ConstVal::Integral(i))),
997997
}
998998
}
999999

@@ -1125,36 +1125,36 @@ fn lit_to_const<'tcx>(lit: &ast::LitKind,
11251125
tcx: &TyCtxt<'tcx>,
11261126
ty_hint: Option<Ty<'tcx>>,
11271127
span: Span,
1128-
) -> Result<ConstVal, ConstEvalErr> {
1128+
) -> Result<ConstVal, ErrKind> {
11291129
use syntax::ast::*;
11301130
use syntax::ast::LitIntType::*;
11311131
match *lit {
11321132
LitKind::Str(ref s, _) => Ok(Str((*s).clone())),
11331133
LitKind::ByteStr(ref data) => Ok(ByteStr(data.clone())),
11341134
LitKind::Byte(n) => Ok(Integral(U8(n))),
11351135
LitKind::Int(n, Signed(ity)) => {
1136-
infer(InferSigned(n as i64), tcx, &ty::TyInt(ity), span).map(Integral)
1136+
infer(InferSigned(n as i64), tcx, &ty::TyInt(ity)).map(Integral)
11371137
},
11381138

11391139
LitKind::Int(n, Unsuffixed) => {
11401140
match ty_hint.map(|t| &t.sty) {
11411141
Some(&ty::TyInt(ity)) => {
1142-
infer(InferSigned(n as i64), tcx, &ty::TyInt(ity), span).map(Integral)
1142+
infer(InferSigned(n as i64), tcx, &ty::TyInt(ity)).map(Integral)
11431143
},
11441144
Some(&ty::TyUint(uty)) => {
1145-
infer(Infer(n), tcx, &ty::TyUint(uty), span).map(Integral)
1145+
infer(Infer(n), tcx, &ty::TyUint(uty)).map(Integral)
11461146
},
11471147
None => Ok(Integral(Infer(n))),
11481148
Some(&ty::TyEnum(ref adt, _)) => {
11491149
let hints = tcx.lookup_repr_hints(adt.did);
11501150
let int_ty = tcx.enum_repr_type(hints.iter().next());
1151-
infer(Infer(n), tcx, &int_ty.to_ty(tcx).sty, span).map(Integral)
1151+
infer(Infer(n), tcx, &int_ty.to_ty(tcx).sty).map(Integral)
11521152
},
11531153
Some(ty_hint) => bug!("bad ty_hint: {:?}, {:?}", ty_hint, lit),
11541154
}
11551155
},
11561156
LitKind::Int(n, Unsigned(ity)) => {
1157-
infer(Infer(n), tcx, &ty::TyUint(ity), span).map(Integral)
1157+
infer(Infer(n), tcx, &ty::TyUint(ity)).map(Integral)
11581158
},
11591159

11601160
LitKind::Float(ref n, _) |

0 commit comments

Comments
 (0)