Skip to content

Commit 049bee0

Browse files
committed
Use more accurate ConstraintCategorys
Adds UseAsConst and UseAsStatic to replace Return in consts/statics. Don't report the arguments to an overloaded operator as CallArguments. Also don't report "escaping data" in these items.
1 parent a2c2487 commit 049bee0

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

src/librustc/ich/impls_mir.rs

+2
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,8 @@ impl_stable_hash_for!(struct mir::ClosureOutlivesRequirement<'tcx> {
557557

558558
impl_stable_hash_for!(enum mir::ConstraintCategory {
559559
Return,
560+
UseAsConst,
561+
UseAsStatic,
560562
TypeAnnotation,
561563
Cast,
562564
ClosureBounds,

src/librustc/mir/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2725,6 +2725,8 @@ pub struct ClosureOutlivesRequirement<'tcx> {
27252725
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
27262726
pub enum ConstraintCategory {
27272727
Return,
2728+
UseAsConst,
2729+
UseAsStatic,
27282730
TypeAnnotation,
27292731
Cast,
27302732

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ impl ConstraintDescription for ConstraintCategory {
4040
match self {
4141
ConstraintCategory::Assignment => "assignment ",
4242
ConstraintCategory::Return => "returning this value ",
43+
ConstraintCategory::UseAsConst => "using this value as a constant ",
44+
ConstraintCategory::UseAsStatic => "using this value as a static ",
4345
ConstraintCategory::Cast => "cast ",
4446
ConstraintCategory::CallArgument => "argument ",
4547
ConstraintCategory::TypeAnnotation => "type annotation ",
@@ -375,12 +377,18 @@ impl<'tcx> RegionInferenceContext<'tcx> {
375377
let outlived_fr_name_and_span =
376378
self.get_var_name_and_span_for_region(infcx.tcx, mir, outlived_fr);
377379

378-
let escapes_from = if infcx.tcx.is_closure(mir_def_id) { "closure" } else { "function" };
380+
let escapes_from = match self.universal_regions.defining_ty {
381+
DefiningTy::Closure(..) => "closure",
382+
DefiningTy::Generator(..) => "generator",
383+
DefiningTy::FnDef(..) => "function",
384+
DefiningTy::Const(..) => "const"
385+
};
379386

380387
// Revert to the normal error in these cases.
381388
// Assignments aren't "escapes" in function items.
382389
if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
383390
|| (category == ConstraintCategory::Assignment && escapes_from == "function")
391+
|| escapes_from == "const"
384392
{
385393
return self.report_general_error(mir, infcx, mir_def_id,
386394
fr, true, outlived_fr, false,

src/librustc_mir/borrow_check/nll/type_check/mod.rs

+42-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use borrow_check::nll::renumber;
2323
use borrow_check::nll::type_check::free_region_relations::{
2424
CreateResult, UniversalRegionRelations,
2525
};
26-
use borrow_check::nll::universal_regions::UniversalRegions;
26+
use borrow_check::nll::universal_regions::{DefiningTy, UniversalRegions};
2727
use borrow_check::nll::ToRegionVid;
2828
use dataflow::move_paths::MoveData;
2929
use dataflow::FlowAtLocation;
@@ -1209,7 +1209,21 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
12091209
// of lowering. Assignments to other sorts of places *are* interesting
12101210
// though.
12111211
let category = match *place {
1212-
Place::Local(RETURN_PLACE) => ConstraintCategory::Return,
1212+
Place::Local(RETURN_PLACE) => if let Some(BorrowCheckContext {
1213+
universal_regions: UniversalRegions {
1214+
defining_ty: DefiningTy::Const(def_id, _),
1215+
..
1216+
},
1217+
..
1218+
}) = self.borrowck_context {
1219+
if tcx.is_static(*def_id).is_some() {
1220+
ConstraintCategory::UseAsStatic
1221+
} else {
1222+
ConstraintCategory::UseAsConst
1223+
}
1224+
} else {
1225+
ConstraintCategory::Return
1226+
}
12131227
Place::Local(l) if !mir.local_decls[l].is_user_variable.is_some() => {
12141228
ConstraintCategory::Boring
12151229
}
@@ -1391,6 +1405,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
13911405
ref func,
13921406
ref args,
13931407
ref destination,
1408+
from_hir_call,
13941409
..
13951410
} => {
13961411
let func_ty = func.ty(mir, tcx);
@@ -1435,7 +1450,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
14351450
}
14361451
}
14371452

1438-
self.check_call_inputs(mir, term, &sig, args, term_location);
1453+
self.check_call_inputs(mir, term, &sig, args, term_location, from_hir_call);
14391454
}
14401455
TerminatorKind::Assert {
14411456
ref cond, ref msg, ..
@@ -1493,7 +1508,23 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
14931508
Some((ref dest, _target_block)) => {
14941509
let dest_ty = dest.ty(mir, tcx).to_ty(tcx);
14951510
let category = match *dest {
1496-
Place::Local(RETURN_PLACE) => ConstraintCategory::Return,
1511+
Place::Local(RETURN_PLACE) => {
1512+
if let Some(BorrowCheckContext {
1513+
universal_regions: UniversalRegions {
1514+
defining_ty: DefiningTy::Const(def_id, _),
1515+
..
1516+
},
1517+
..
1518+
}) = self.borrowck_context {
1519+
if tcx.is_static(*def_id).is_some() {
1520+
ConstraintCategory::UseAsStatic
1521+
} else {
1522+
ConstraintCategory::UseAsConst
1523+
}
1524+
} else {
1525+
ConstraintCategory::Return
1526+
}
1527+
},
14971528
Place::Local(l) if !mir.local_decls[l].is_user_variable.is_some() => {
14981529
ConstraintCategory::Boring
14991530
}
@@ -1538,18 +1569,24 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
15381569
sig: &ty::FnSig<'tcx>,
15391570
args: &[Operand<'tcx>],
15401571
term_location: Location,
1572+
from_hir_call: bool,
15411573
) {
15421574
debug!("check_call_inputs({:?}, {:?})", sig, args);
15431575
if args.len() < sig.inputs().len() || (args.len() > sig.inputs().len() && !sig.variadic) {
15441576
span_mirbug!(self, term, "call to {:?} with wrong # of args", sig);
15451577
}
15461578
for (n, (fn_arg, op_arg)) in sig.inputs().iter().zip(args).enumerate() {
15471579
let op_arg_ty = op_arg.ty(mir, self.tcx());
1580+
let category = if from_hir_call {
1581+
ConstraintCategory::CallArgument
1582+
} else {
1583+
ConstraintCategory::Boring
1584+
};
15481585
if let Err(terr) = self.sub_types(
15491586
op_arg_ty,
15501587
fn_arg,
15511588
term_location.to_locations(),
1552-
ConstraintCategory::CallArgument,
1589+
category,
15531590
) {
15541591
span_mirbug!(
15551592
self,

0 commit comments

Comments
 (0)