Skip to content

Commit 705e087

Browse files
committed
reduce code duplication
1 parent ffcdbad commit 705e087

File tree

2 files changed

+38
-45
lines changed

2 files changed

+38
-45
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,39 +1303,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13031303
_ => {}
13041304
}
13051305

1306-
/// This is a bare signal of what kind of type we're dealing with. `ty::TyKind` tracks
1307-
/// extra information about each type, but we only care about the category.
1308-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1309-
enum TyCategory {
1310-
Closure,
1311-
Opaque,
1312-
Generator,
1313-
Foreign,
1314-
}
1315-
1316-
impl TyCategory {
1317-
fn descr(&self) -> &'static str {
1318-
match self {
1319-
Self::Closure => "closure",
1320-
Self::Opaque => "opaque type",
1321-
Self::Generator => "generator",
1322-
Self::Foreign => "foreign type",
1323-
}
1324-
}
1325-
1326-
fn from_ty(ty: Ty<'_>) -> Option<(Self, DefId)> {
1327-
match ty.kind {
1328-
ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
1329-
ty::Opaque(def_id, _) => Some((Self::Opaque, def_id)),
1330-
ty::Generator(def_id, ..) => Some((Self::Generator, def_id)),
1331-
ty::Foreign(def_id) => Some((Self::Foreign, def_id)),
1332-
_ => None,
1333-
}
1334-
}
1335-
}
1336-
13371306
struct OpaqueTypesVisitor<'tcx> {
1338-
types: FxHashMap<TyKind, FxHashSet<Span>>,
1307+
types: FxHashMap<TyCategory, FxHashSet<Span>>,
13391308
expected: FxHashMap<TyCategory, FxHashSet<Span>>,
13401309
found: FxHashMap<TyCategory, FxHashSet<Span>>,
13411310
ignore_span: Span,
@@ -1375,7 +1344,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13751344
&self,
13761345
err: &mut DiagnosticBuilder<'_>,
13771346
target: &str,
1378-
types: &FxHashMap<TyKind, FxHashSet<Span>>,
1347+
types: &FxHashMap<TyCategory, FxHashSet<Span>>,
13791348
) {
13801349
for (key, values) in types.iter() {
13811350
let count = values.len();
@@ -1394,7 +1363,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13941363
},
13951364
if count > 1 { "one of the " } else { "" },
13961365
target,
1397-
key,
1366+
kind,
13981367
pluralize!(count),
13991368
),
14001369
);
@@ -1405,7 +1374,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14051374

14061375
impl<'tcx> ty::fold::TypeVisitor<'tcx> for OpaqueTypesVisitor<'tcx> {
14071376
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
1408-
if let Some((kind, def_id)) = TyKind::from_ty(t) {
1377+
if let Some((kind, def_id)) = TyCategory::from_ty(t) {
14091378
let span = self.tcx.def_span(def_id);
14101379
// Avoid cluttering the output when the "found" and error span overlap:
14111380
//
@@ -2067,3 +2036,34 @@ impl<'tcx> ObligationCause<'tcx> {
20672036
}
20682037
}
20692038
}
2039+
2040+
/// This is a bare signal of what kind of type we're dealing with. `ty::TyKind` tracks
2041+
/// extra information about each type, but we only care about the category.
2042+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2043+
crate enum TyCategory {
2044+
Closure,
2045+
Opaque,
2046+
Generator,
2047+
Foreign,
2048+
}
2049+
2050+
impl TyCategory {
2051+
fn descr(&self) -> &'static str {
2052+
match self {
2053+
Self::Closure => "closure",
2054+
Self::Opaque => "opaque type",
2055+
Self::Generator => "generator",
2056+
Self::Foreign => "foreign type",
2057+
}
2058+
}
2059+
2060+
pub fn from_ty(ty: Ty<'_>) -> Option<(Self, DefId)> {
2061+
match ty.kind {
2062+
ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
2063+
ty::Opaque(def_id, _) => Some((Self::Opaque, def_id)),
2064+
ty::Generator(def_id, ..) => Some((Self::Generator, def_id)),
2065+
ty::Foreign(def_id) => Some((Self::Foreign, def_id)),
2066+
_ => None,
2067+
}
2068+
}
2069+
}

src/librustc/traits/error_reporting.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::{
66
TraitNotObjectSafe,
77
};
88

9-
use crate::infer::error_reporting::TypeAnnotationNeeded as ErrorCode;
9+
use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
1010
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1111
use crate::infer::{self, InferCtxt};
1212
use crate::mir::interpret::ErrorHandled;
@@ -676,15 +676,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
676676
Some(t) => Some(t),
677677
None => {
678678
let ty = parent_trait_ref.skip_binder().self_ty();
679-
let span = if let ty::Closure(def_id, _)
680-
| ty::Opaque(def_id, _)
681-
| ty::Generator(def_id, ..)
682-
| ty::Foreign(def_id) = ty.kind
683-
{
684-
Some(self.tcx.def_span(def_id))
685-
} else {
686-
None
687-
};
679+
let span =
680+
TyCategory::from_ty(ty).map(|(_, def_id)| self.tcx.def_span(def_id));
688681
Some((ty.to_string(), span))
689682
}
690683
}

0 commit comments

Comments
 (0)