Skip to content

Commit b7cbbc3

Browse files
Ariel Ben-Yehudaarielb1
authored andcommitted
be more type-safe in panic/panic_bounds_check
TODO: find a correct borrow region Fixes #31482
1 parent 67d1cf1 commit b7cbbc3

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/librustc_mir/build/scope.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -418,25 +418,28 @@ impl<'a,'tcx> Builder<'a,'tcx> {
418418
len: Operand<'tcx>,
419419
span: Span) {
420420
// fn(&(filename: &'static str, line: u32), index: usize, length: usize) -> !
421+
let region = ty::ReStatic; // TODO(mir-borrowck): use a better region?
421422
let func = self.lang_function(lang_items::PanicBoundsCheckFnLangItem);
422-
let args = func.ty.fn_args();
423-
let ref_ty = args.skip_binder()[0];
424-
let (region, tup_ty) = if let ty::TyRef(region, tyandmut) = ref_ty.sty {
425-
(region, tyandmut.ty)
423+
let args = self.hir.tcx().replace_late_bound_regions(&func.ty.fn_args(), |_| region).0;
424+
425+
let ref_ty = args[0];
426+
let tup_ty = if let ty::TyRef(_, tyandmut) = ref_ty.sty {
427+
tyandmut.ty
426428
} else {
427429
self.hir.span_bug(span, &format!("unexpected panic_bound_check type: {:?}", func.ty));
428430
};
431+
429432
let (tuple, tuple_ref) = (self.temp(tup_ty), self.temp(ref_ty));
430433
let (file, line) = self.span_to_fileline_args(span);
431434
let elems = vec![Operand::Constant(file), Operand::Constant(line)];
432435
// FIXME: We should have this as a constant, rather than a stack variable (to not pollute
433436
// icache with cold branch code), however to achieve that we either have to rely on rvalue
434437
// promotion or have some way, in MIR, to create constants.
435-
self.cfg.push_assign(block, DUMMY_SP, &tuple, // tuple = (file_arg, line_arg);
438+
self.cfg.push_assign(block, span, &tuple, // tuple = (file_arg, line_arg);
436439
Rvalue::Aggregate(AggregateKind::Tuple, elems));
437440
// FIXME: is this region really correct here?
438-
self.cfg.push_assign(block, DUMMY_SP, &tuple_ref, // tuple_ref = &tuple;
439-
Rvalue::Ref(*region, BorrowKind::Unique, tuple));
441+
self.cfg.push_assign(block, span, &tuple_ref, // tuple_ref = &tuple;
442+
Rvalue::Ref(region, BorrowKind::Shared, tuple));
440443
let cleanup = self.diverge_cleanup();
441444
self.cfg.terminate(block, Terminator::Call {
442445
func: Operand::Constant(func),
@@ -449,18 +452,21 @@ impl<'a,'tcx> Builder<'a,'tcx> {
449452
/// Create diverge cleanup and branch to it from `block`.
450453
pub fn panic(&mut self, block: BasicBlock, msg: &'static str, span: Span) {
451454
// fn(&(msg: &'static str filename: &'static str, line: u32)) -> !
455+
let region = ty::ReStatic; // TODO(mir-borrowck): use a better region?
452456
let func = self.lang_function(lang_items::PanicFnLangItem);
453-
let args = func.ty.fn_args();
454-
let ref_ty = args.skip_binder()[0];
455-
let (region, tup_ty) = if let ty::TyRef(region, tyandmut) = ref_ty.sty {
456-
(region, tyandmut.ty)
457+
let args = self.hir.tcx().replace_late_bound_regions(&func.ty.fn_args(), |_| region).0;
458+
459+
let ref_ty = args[0];
460+
let tup_ty = if let ty::TyRef(_, tyandmut) = ref_ty.sty {
461+
tyandmut.ty
457462
} else {
458463
self.hir.span_bug(span, &format!("unexpected panic type: {:?}", func.ty));
459464
};
465+
460466
let (tuple, tuple_ref) = (self.temp(tup_ty), self.temp(ref_ty));
461467
let (file, line) = self.span_to_fileline_args(span);
462468
let message = Constant {
463-
span: DUMMY_SP,
469+
span: span,
464470
ty: self.hir.tcx().mk_static_str(),
465471
literal: self.hir.str_literal(intern_and_get_ident(msg))
466472
};
@@ -470,11 +476,11 @@ impl<'a,'tcx> Builder<'a,'tcx> {
470476
// FIXME: We should have this as a constant, rather than a stack variable (to not pollute
471477
// icache with cold branch code), however to achieve that we either have to rely on rvalue
472478
// promotion or have some way, in MIR, to create constants.
473-
self.cfg.push_assign(block, DUMMY_SP, &tuple, // tuple = (message_arg, file_arg, line_arg);
479+
self.cfg.push_assign(block, span, &tuple, // tuple = (message_arg, file_arg, line_arg);
474480
Rvalue::Aggregate(AggregateKind::Tuple, elems));
475481
// FIXME: is this region really correct here?
476-
self.cfg.push_assign(block, DUMMY_SP, &tuple_ref, // tuple_ref = &tuple;
477-
Rvalue::Ref(*region, BorrowKind::Unique, tuple));
482+
self.cfg.push_assign(block, span, &tuple_ref, // tuple_ref = &tuple;
483+
Rvalue::Ref(region, BorrowKind::Shared, tuple));
478484
let cleanup = self.diverge_cleanup();
479485
self.cfg.terminate(block, Terminator::Call {
480486
func: Operand::Constant(func),
@@ -505,11 +511,11 @@ impl<'a,'tcx> Builder<'a,'tcx> {
505511
fn span_to_fileline_args(&mut self, span: Span) -> (Constant<'tcx>, Constant<'tcx>) {
506512
let span_lines = self.hir.tcx().sess.codemap().lookup_char_pos(span.lo);
507513
(Constant {
508-
span: DUMMY_SP,
514+
span: span,
509515
ty: self.hir.tcx().mk_static_str(),
510516
literal: self.hir.str_literal(intern_and_get_ident(&span_lines.file.name))
511517
}, Constant {
512-
span: DUMMY_SP,
518+
span: span,
513519
ty: self.hir.tcx().types.u32,
514520
literal: self.hir.usize_literal(span_lines.line)
515521
})

0 commit comments

Comments
 (0)