Skip to content

Commit 9c54d86

Browse files
committed
Stop calling bug() in various weird cases and instead generate Err().
1 parent f7abf47 commit 9c54d86

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

src/librustc/middle/mem_categorization.rs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -195,51 +195,39 @@ pub enum deref_kind {
195195
// Categorizes a derefable type. Note that we include vectors and strings as
196196
// derefable (we model an index as the combination of a deref and then a
197197
// pointer adjustment).
198-
pub fn opt_deref_kind(t: Ty) -> Option<deref_kind> {
198+
pub fn deref_kind(t: Ty) -> McResult<deref_kind> {
199199
match t.sty {
200200
ty::ty_uniq(_) |
201201
ty::ty_closure(box ty::ClosureTy {store: ty::UniqTraitStore, ..}) => {
202-
Some(deref_ptr(Unique))
202+
Ok(deref_ptr(Unique))
203203
}
204204

205205
ty::ty_rptr(r, mt) => {
206206
let kind = ty::BorrowKind::from_mutbl(mt.mutbl);
207-
Some(deref_ptr(BorrowedPtr(kind, *r)))
207+
Ok(deref_ptr(BorrowedPtr(kind, *r)))
208208
}
209209

210210
ty::ty_closure(box ty::ClosureTy {
211211
store: ty::RegionTraitStore(r, _),
212212
..
213213
}) => {
214-
Some(deref_ptr(BorrowedPtr(ty::ImmBorrow, r)))
214+
Ok(deref_ptr(BorrowedPtr(ty::ImmBorrow, r)))
215215
}
216216

217217
ty::ty_ptr(ref mt) => {
218-
Some(deref_ptr(UnsafePtr(mt.mutbl)))
218+
Ok(deref_ptr(UnsafePtr(mt.mutbl)))
219219
}
220220

221221
ty::ty_enum(..) |
222222
ty::ty_struct(..) => { // newtype
223-
Some(deref_interior(InteriorField(PositionalField(0))))
223+
Ok(deref_interior(InteriorField(PositionalField(0))))
224224
}
225225

226226
ty::ty_vec(_, _) | ty::ty_str => {
227-
Some(deref_interior(InteriorElement(element_kind(t))))
227+
Ok(deref_interior(InteriorElement(element_kind(t))))
228228
}
229229

230-
_ => None
231-
}
232-
}
233-
234-
pub fn deref_kind<'tcx>(tcx: &ty::ctxt<'tcx>, t: Ty<'tcx>) -> deref_kind {
235-
debug!("deref_kind {}", ty_to_string(tcx, t));
236-
match opt_deref_kind(t) {
237-
Some(k) => k,
238-
None => {
239-
tcx.sess.bug(
240-
format!("deref_kind() invoked on non-derefable type {}",
241-
ty_to_string(tcx, t))[]);
242-
}
230+
_ => Err(()),
243231
}
244232
}
245233

@@ -403,7 +391,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
403391

404392
fn pat_ty(&self, pat: &ast::Pat) -> McResult<Ty<'tcx>> {
405393
let tcx = self.typer.tcx();
406-
let base_ty = self.typer.node_ty(pat.id);
394+
let base_ty = try!(self.typer.node_ty(pat.id));
407395
// FIXME (Issue #18207): This code detects whether we are
408396
// looking at a `ref x`, and if so, figures out what the type
409397
// *being borrowed* is. But ideally we would put in a more
@@ -413,15 +401,16 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
413401
// a bind-by-ref means that the base_ty will be the type of the ident itself,
414402
// but what we want here is the type of the underlying value being borrowed.
415403
// So peel off one-level, turning the &T into T.
416-
ty::deref(base_ty, false).unwrap_or_else(|| {
417-
panic!("encountered BindByRef with non &-type");
418-
}).ty
404+
match ty::deref(base_ty, false) {
405+
Some(t) => t.ty,
406+
None => { return Err(()); }
407+
}
419408
}
420409
_ => base_ty,
421410
};
422411
debug!("pat_ty(pat={}) base_ty={} ret_ty={}",
423412
pat.repr(tcx), base_ty.repr(tcx), ret_ty.repr(tcx));
424-
ret_ty
413+
Ok(ret_ty)
425414
}
426415

427416
pub fn cat_expr(&self, expr: &ast::Expr) -> McResult<cmt<'tcx>> {
@@ -909,13 +898,13 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
909898
}
910899
None => base_cmt
911900
};
912-
match ty::deref(base_cmt.ty, true) {
901+
let base_cmt_ty = base_cmt.ty;
902+
match ty::deref(base_cmt_ty, true) {
913903
Some(mt) => self.cat_deref_common(node, base_cmt, deref_cnt, mt.ty, implicit),
914904
None => {
915-
self.tcx().sess.span_bug(
916-
node.span(),
917-
format!("Explicit deref of non-derefable type: {}",
918-
base_cmt.ty.repr(self.tcx()))[]);
905+
debug!("Explicit deref of non-derefable type: {}",
906+
base_cmt_ty.repr(self.tcx()));
907+
return Err(());
919908
}
920909
}
921910
}
@@ -992,17 +981,14 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
992981
match ty::array_element_ty(self.tcx(), base_cmt.ty) {
993982
Some(ty) => ty,
994983
None => {
995-
self.tcx().sess.span_bug(
996-
elt.span(),
997-
format!("Explicit index of non-index type `{}`",
998-
base_cmt.ty.repr(self.tcx()))[]);
984+
return Err(());
999985
}
1000986
}
1001987
}
1002988
};
1003989

1004990
let m = base_cmt.mutbl.inherit();
1005-
return interior(elt, base_cmt.clone(), base_cmt.ty, m, element_ty);
991+
return Ok(interior(elt, base_cmt.clone(), base_cmt.ty, m, element_ty));
1006992

1007993
fn interior<'tcx, N: ast_node>(elt: &N,
1008994
of_cmt: cmt<'tcx>,

0 commit comments

Comments
 (0)