Skip to content

Commit c6e130e

Browse files
committed
rustc_const_eval: convert constants to Pattern instead of hir::Pat.
1 parent c001b09 commit c6e130e

File tree

3 files changed

+180
-188
lines changed

3 files changed

+180
-188
lines changed

src/librustc_const_eval/check_match.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,6 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
116116
fn report_inlining_errors(&self, patcx: PatternContext, pat_span: Span) {
117117
for error in patcx.errors {
118118
match error {
119-
PatternError::BadConstInPattern(span, def_id) => {
120-
self.tcx.sess.span_err(
121-
span,
122-
&format!("constants of the type `{}` \
123-
cannot be used in patterns",
124-
self.tcx.item_path_str(def_id)));
125-
}
126119
PatternError::StaticInPattern(span) => {
127120
span_err!(self.tcx.sess, span, E0158,
128121
"statics cannot be referenced in patterns");

src/librustc_const_eval/eval.rs

Lines changed: 3 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,20 @@ use self::EvalHint::*;
1818
use rustc::hir::map as ast_map;
1919
use rustc::hir::map::blocks::FnLikeNode;
2020
use rustc::traits;
21-
use rustc::hir::def::{Def, CtorKind};
21+
use rustc::hir::def::Def;
2222
use rustc::hir::def_id::DefId;
2323
use rustc::ty::{self, Ty, TyCtxt};
2424
use rustc::ty::util::IntTypeExt;
2525
use rustc::ty::subst::Substs;
2626
use rustc::traits::Reveal;
2727
use rustc::util::common::ErrorReported;
2828
use rustc::util::nodemap::DefIdMap;
29-
use rustc::lint;
3029

3130
use graphviz::IntoCow;
3231
use syntax::ast;
33-
use rustc::hir::{Expr, PatKind};
34-
use rustc::hir;
35-
use syntax::ptr::P;
36-
use syntax::codemap;
32+
use rustc::hir::{self, Expr};
3733
use syntax::attr::IntType;
38-
use syntax_pos::{self, Span};
34+
use syntax_pos::Span;
3935

4036
use std::borrow::Cow;
4137
use std::cmp::Ordering;
@@ -186,126 +182,6 @@ fn lookup_const_fn_by_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
186182
}
187183
}
188184

189-
pub fn const_expr_to_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
190-
expr: &Expr,
191-
pat_id: ast::NodeId,
192-
span: Span)
193-
-> Result<P<hir::Pat>, DefId> {
194-
let pat_ty = tcx.tables().expr_ty(expr);
195-
debug!("expr={:?} pat_ty={:?} pat_id={}", expr, pat_ty, pat_id);
196-
match pat_ty.sty {
197-
ty::TyFloat(_) => {
198-
tcx.sess.add_lint(
199-
lint::builtin::ILLEGAL_FLOATING_POINT_CONSTANT_PATTERN,
200-
pat_id,
201-
span,
202-
format!("floating point constants cannot be used in patterns"));
203-
}
204-
ty::TyAdt(adt_def, _) if adt_def.is_union() => {
205-
// Matching on union fields is unsafe, we can't hide it in constants
206-
tcx.sess.span_err(span, "cannot use unions in constant patterns");
207-
}
208-
ty::TyAdt(adt_def, _) => {
209-
if !tcx.has_attr(adt_def.did, "structural_match") {
210-
tcx.sess.add_lint(
211-
lint::builtin::ILLEGAL_STRUCT_OR_ENUM_CONSTANT_PATTERN,
212-
pat_id,
213-
span,
214-
format!("to use a constant of type `{}` \
215-
in a pattern, \
216-
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
217-
tcx.item_path_str(adt_def.did),
218-
tcx.item_path_str(adt_def.did)));
219-
}
220-
}
221-
_ => { }
222-
}
223-
let pat = match expr.node {
224-
hir::ExprTup(ref exprs) =>
225-
PatKind::Tuple(exprs.iter()
226-
.map(|expr| const_expr_to_pat(tcx, &expr, pat_id, span))
227-
.collect::<Result<_, _>>()?, None),
228-
229-
hir::ExprCall(ref callee, ref args) => {
230-
let qpath = match callee.node {
231-
hir::ExprPath(ref qpath) => qpath,
232-
_ => bug!()
233-
};
234-
let def = tcx.tables().qpath_def(qpath, callee.id);
235-
let ctor_path = if let hir::QPath::Resolved(_, ref path) = *qpath {
236-
match def {
237-
Def::StructCtor(_, CtorKind::Fn) |
238-
Def::VariantCtor(_, CtorKind::Fn) => {
239-
Some(path.clone())
240-
}
241-
_ => None
242-
}
243-
} else {
244-
None
245-
};
246-
match (def, ctor_path) {
247-
(Def::Fn(..), None) | (Def::Method(..), None) => {
248-
PatKind::Lit(P(expr.clone()))
249-
}
250-
(_, Some(ctor_path)) => {
251-
let pats = args.iter()
252-
.map(|expr| const_expr_to_pat(tcx, expr, pat_id, span))
253-
.collect::<Result<_, _>>()?;
254-
PatKind::TupleStruct(hir::QPath::Resolved(None, ctor_path), pats, None)
255-
}
256-
_ => bug!()
257-
}
258-
}
259-
260-
hir::ExprStruct(ref qpath, ref fields, None) => {
261-
let field_pats =
262-
fields.iter()
263-
.map(|field| Ok(codemap::Spanned {
264-
span: syntax_pos::DUMMY_SP,
265-
node: hir::FieldPat {
266-
name: field.name.node,
267-
pat: const_expr_to_pat(tcx, &field.expr, pat_id, span)?,
268-
is_shorthand: false,
269-
},
270-
}))
271-
.collect::<Result<_, _>>()?;
272-
PatKind::Struct(qpath.clone(), field_pats, false)
273-
}
274-
275-
hir::ExprArray(ref exprs) => {
276-
let pats = exprs.iter()
277-
.map(|expr| const_expr_to_pat(tcx, &expr, pat_id, span))
278-
.collect::<Result<_, _>>()?;
279-
PatKind::Slice(pats, None, hir::HirVec::new())
280-
}
281-
282-
hir::ExprPath(ref qpath) => {
283-
let def = tcx.tables().qpath_def(qpath, expr.id);
284-
match def {
285-
Def::StructCtor(_, CtorKind::Const) |
286-
Def::VariantCtor(_, CtorKind::Const) => {
287-
match expr.node {
288-
hir::ExprPath(hir::QPath::Resolved(_, ref path)) => {
289-
PatKind::Path(hir::QPath::Resolved(None, path.clone()))
290-
}
291-
_ => bug!()
292-
}
293-
}
294-
Def::Const(def_id) | Def::AssociatedConst(def_id) => {
295-
let substs = Some(tcx.tables().node_id_item_substs(expr.id)
296-
.unwrap_or_else(|| tcx.intern_substs(&[])));
297-
let (expr, _ty) = lookup_const_by_id(tcx, def_id, substs).unwrap();
298-
return const_expr_to_pat(tcx, expr, pat_id, span);
299-
},
300-
_ => bug!(),
301-
}
302-
}
303-
304-
_ => PatKind::Lit(P(expr.clone()))
305-
};
306-
Ok(P(hir::Pat { id: expr.id, node: pat, span: span }))
307-
}
308-
309185
pub fn report_const_eval_err<'a, 'tcx>(
310186
tcx: TyCtxt<'a, 'tcx, 'tcx>,
311187
err: &ConstEvalErr,

0 commit comments

Comments
 (0)