Skip to content

Commit 7f7709e

Browse files
committed
Generalized article_and_description
1 parent 3eeefc2 commit 7f7709e

File tree

5 files changed

+83
-14
lines changed

5 files changed

+83
-14
lines changed

src/librustc/ty/context.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn validate_hir_id_for_typeck_tables(
209209
ty::tls::with(|tcx| {
210210
bug!(
211211
"node {} with HirId::owner {:?} cannot be placed in \
212-
TypeckTables with local_id_root {:?}",
212+
TypeckTables with local_id_root {:?}",
213213
tcx.hir().node_to_string(hir_id),
214214
DefId::local(hir_id.owner),
215215
local_id_root
@@ -1512,6 +1512,20 @@ impl<'tcx> TyCtxt<'tcx> {
15121512
.subst(*self, self.mk_substs([self.lifetimes.re_static.into()].iter())),
15131513
)
15141514
}
1515+
1516+
/// Returns a displayable description and article for the given `def_id` (e.g. `("a", "closure")`).
1517+
pub fn article_and_description(
1518+
&self,
1519+
def_id: crate::hir::def_id::DefId,
1520+
) -> (&'static str, &'static str) {
1521+
self.def_kind(def_id).map_or_else(
1522+
|| {
1523+
// TODO: is it a problem to try to use the ty here?
1524+
self.type_of(def_id).kind.article_and_description()
1525+
},
1526+
|def_kind| (def_kind.article(), def_kind.descr(def_id)),
1527+
)
1528+
}
15151529
}
15161530

15171531
impl<'tcx> GlobalCtxt<'tcx> {

src/librustc/ty/sty.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,41 @@ pub enum TyKind<'tcx> {
254254
Error,
255255
}
256256

257+
impl<'tcx> TyKind<'tcx> {
258+
pub fn article_and_description(&self) -> (&'static str, &'static str) {
259+
match *self {
260+
Bool => ("a", "boolean value"),
261+
Char => ("a", "character"),
262+
Int(..) => ("a", "signed interger"),
263+
Uint(..) => ("an", "unsigned integer"),
264+
Float(..) => ("a", "floating point number"),
265+
Adt(..) => ("an", "abstract data type"),
266+
Foreign(..) => ("a", "foreign type"),
267+
Str => ("a", "string slice"),
268+
Array(..) => ("an", "array"),
269+
Slice(..) => ("a", "slice"),
270+
RawPtr(..) => ("a", "raw pointer"),
271+
Ref(..) => ("a", "reference"),
272+
FnDef(..) => ("a", "function"),
273+
FnPtr(..) => ("a", "function pointer"),
274+
Dynamic(..) => ("a", "trait object"),
275+
Closure(..) => ("a", "closure"),
276+
Generator(..) => ("a", "generator"),
277+
GeneratorWitness(..) => ("a", "generator witness"),
278+
Never => ("a", "never"),
279+
Tuple(..) => ("a", "tuple"),
280+
Projection(..) => ("a", "projection"),
281+
UnnormalizedProjection(..) => ("an", "unnormalized projection"),
282+
Opaque(..) => ("an", "opaque type"),
283+
Param(..) => ("a", "type parameter"),
284+
Bound(..) => ("a", "bound type variable"),
285+
Placeholder(..) => ("a", "universally quantified higher-ranked type"),
286+
Infer(..) => ("an", "inference variable"),
287+
Error => ("a", "type error"),
288+
}
289+
}
290+
}
291+
257292
// `TyKind` is used a lot. Make sure it doesn't unintentionally get bigger.
258293
#[cfg(target_arch = "x86_64")]
259294
static_assert_size!(TyKind<'_>, 24);

src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12571257
}
12581258
_ => bug!(
12591259
"report_escaping_closure_capture called with unexpected constraint \
1260-
category: `{:?}`",
1260+
category: `{:?}`",
12611261
category
12621262
),
12631263
};
@@ -1279,8 +1279,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12791279
let tables = tcx.typeck_tables_of(self.mir_def_id);
12801280
let mir_hir_id = tcx.hir().def_index_to_hir_id(self.mir_def_id.index);
12811281
match tables.node_type(mir_hir_id).kind {
1282-
ty::Closure(..) => "closure",
1283-
ty::Generator(..) => "generator",
1282+
ref kind @ ty::Closure(..) | ref kind @ ty::Generator(..) => {
1283+
kind.article_and_description().1
1284+
}
12841285
_ => bug!("Closure body doesn't have a closure or generator type"),
12851286
}
12861287
} else {

src/librustc_mir/borrow_check/diagnostics/region_errors.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -427,18 +427,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
427427
errci.outlived_fr,
428428
);
429429

430-
let escapes_from = match self.regioncx.universal_regions().defining_ty {
431-
DefiningTy::Closure(..) => "closure",
432-
DefiningTy::Generator(..) => "generator",
433-
DefiningTy::FnDef(..) => "function",
434-
DefiningTy::Const(..) => "const",
435-
};
430+
let (_, escapes_from) =
431+
self.infcx.tcx.article_and_description(self.universal_regions.defining_ty.def_id());
436432

437433
// Revert to the normal error in these cases.
438434
// Assignments aren't "escapes" in function items.
439435
if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
440-
|| (*category == ConstraintCategory::Assignment && escapes_from == "function")
441-
|| escapes_from == "const"
436+
|| (*category == ConstraintCategory::Assignment
437+
&& self.universal_regions.defining_ty.is_fn_def())
438+
|| self.universal_regions.defining_ty.is_closure()
442439
{
443440
return self.report_general_error(&ErrorConstraintInfo {
444441
fr_is_local: true,
@@ -504,8 +501,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
504501
let mut diag =
505502
self.infcx.tcx.sess.struct_span_err(*span, "lifetime may not live long enough");
506503

507-
let mir_def_name =
508-
if self.infcx.tcx.is_closure(self.mir_def_id) { "closure" } else { "function" };
504+
let (_, mir_def_name) = self.infcx.tcx.article_and_description(self.mir_def_id);
509505

510506
let fr_name = self.give_region_a_name(*fr).unwrap();
511507
fr_name.highlight_region_name(&mut diag);

src/librustc_mir/borrow_check/universal_regions.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,29 @@ impl<'tcx> DefiningTy<'tcx> {
131131
DefiningTy::FnDef(..) | DefiningTy::Const(..) => 0,
132132
}
133133
}
134+
135+
pub fn is_closure(&self) -> bool {
136+
match *self {
137+
DefiningTy::Closure(..) => true,
138+
_ => false,
139+
}
140+
}
141+
142+
pub fn is_fn_def(&self) -> bool {
143+
match *self {
144+
DefiningTy::FnDef(..) => true,
145+
_ => false,
146+
}
147+
}
148+
149+
pub fn def_id(&self) -> DefId {
150+
match *self {
151+
DefiningTy::Closure(def_id, ..) => def_id,
152+
DefiningTy::Generator(def_id, ..) => def_id,
153+
DefiningTy::FnDef(def_id, ..) => def_id,
154+
DefiningTy::Const(def_id, ..) => def_id,
155+
}
156+
}
134157
}
135158

136159
#[derive(Debug)]

0 commit comments

Comments
 (0)