Skip to content

Commit

Permalink
reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Jan 8, 2020
1 parent ffcdbad commit 705e087
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 45 deletions.
70 changes: 35 additions & 35 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,39 +1303,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
_ => {}
}

/// This is a bare signal of what kind of type we're dealing with. `ty::TyKind` tracks
/// extra information about each type, but we only care about the category.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum TyCategory {
Closure,
Opaque,
Generator,
Foreign,
}

impl TyCategory {
fn descr(&self) -> &'static str {
match self {
Self::Closure => "closure",
Self::Opaque => "opaque type",
Self::Generator => "generator",
Self::Foreign => "foreign type",
}
}

fn from_ty(ty: Ty<'_>) -> Option<(Self, DefId)> {
match ty.kind {
ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
ty::Opaque(def_id, _) => Some((Self::Opaque, def_id)),
ty::Generator(def_id, ..) => Some((Self::Generator, def_id)),
ty::Foreign(def_id) => Some((Self::Foreign, def_id)),
_ => None,
}
}
}

struct OpaqueTypesVisitor<'tcx> {
types: FxHashMap<TyKind, FxHashSet<Span>>,
types: FxHashMap<TyCategory, FxHashSet<Span>>,
expected: FxHashMap<TyCategory, FxHashSet<Span>>,
found: FxHashMap<TyCategory, FxHashSet<Span>>,
ignore_span: Span,
Expand Down Expand Up @@ -1375,7 +1344,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
&self,
err: &mut DiagnosticBuilder<'_>,
target: &str,
types: &FxHashMap<TyKind, FxHashSet<Span>>,
types: &FxHashMap<TyCategory, FxHashSet<Span>>,
) {
for (key, values) in types.iter() {
let count = values.len();
Expand All @@ -1394,7 +1363,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
},
if count > 1 { "one of the " } else { "" },
target,
key,
kind,
pluralize!(count),
),
);
Expand All @@ -1405,7 +1374,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

impl<'tcx> ty::fold::TypeVisitor<'tcx> for OpaqueTypesVisitor<'tcx> {
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
if let Some((kind, def_id)) = TyKind::from_ty(t) {
if let Some((kind, def_id)) = TyCategory::from_ty(t) {
let span = self.tcx.def_span(def_id);
// Avoid cluttering the output when the "found" and error span overlap:
//
Expand Down Expand Up @@ -2067,3 +2036,34 @@ impl<'tcx> ObligationCause<'tcx> {
}
}
}

/// This is a bare signal of what kind of type we're dealing with. `ty::TyKind` tracks
/// extra information about each type, but we only care about the category.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
crate enum TyCategory {
Closure,
Opaque,
Generator,
Foreign,
}

impl TyCategory {
fn descr(&self) -> &'static str {
match self {
Self::Closure => "closure",
Self::Opaque => "opaque type",
Self::Generator => "generator",
Self::Foreign => "foreign type",
}
}

pub fn from_ty(ty: Ty<'_>) -> Option<(Self, DefId)> {
match ty.kind {
ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
ty::Opaque(def_id, _) => Some((Self::Opaque, def_id)),
ty::Generator(def_id, ..) => Some((Self::Generator, def_id)),
ty::Foreign(def_id) => Some((Self::Foreign, def_id)),
_ => None,
}
}
}
13 changes: 3 additions & 10 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
TraitNotObjectSafe,
};

use crate::infer::error_reporting::TypeAnnotationNeeded as ErrorCode;
use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::{self, InferCtxt};
use crate::mir::interpret::ErrorHandled;
Expand Down Expand Up @@ -676,15 +676,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
Some(t) => Some(t),
None => {
let ty = parent_trait_ref.skip_binder().self_ty();
let span = if let ty::Closure(def_id, _)
| ty::Opaque(def_id, _)
| ty::Generator(def_id, ..)
| ty::Foreign(def_id) = ty.kind
{
Some(self.tcx.def_span(def_id))
} else {
None
};
let span =
TyCategory::from_ty(ty).map(|(_, def_id)| self.tcx.def_span(def_id));
Some((ty.to_string(), span))
}
}
Expand Down

0 comments on commit 705e087

Please sign in to comment.