Extract out error message generation.

This commit is contained in:
Corey Farwell 2017-02-19 13:00:25 -05:00
parent a97aed739b
commit 2436d7374c
2 changed files with 21 additions and 33 deletions

View file

@ -21,7 +21,6 @@ use super::{
SelectionContext,
SelectionError,
ObjectSafetyViolation,
MethodViolationCode,
};
use fmt_macros::{Parser, Piece, Position};
@ -679,38 +678,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
if !reported_violations.insert(violation.clone()) {
continue;
}
let buf;
let note = match violation {
ObjectSafetyViolation::SizedSelf => {
"the trait cannot require that `Self : Sized`"
}
ObjectSafetyViolation::SupertraitSelf => {
"the trait cannot use `Self` as a type parameter \
in the supertrait listing"
}
ObjectSafetyViolation::Method(name,
MethodViolationCode::StaticMethod) => {
buf = format!("method `{}` has no receiver", name);
&buf
}
ObjectSafetyViolation::Method(name,
MethodViolationCode::ReferencesSelf) => {
buf = format!("method `{}` references the `Self` type \
in its arguments or return type",
name);
&buf
}
ObjectSafetyViolation::Method(name,
MethodViolationCode::Generic) => {
buf = format!("method `{}` has generic type parameters", name);
&buf
}
};
err.note(note);
err.note(&violation.error_msg());
}
err
}

View file

@ -23,6 +23,7 @@ use hir::def_id::DefId;
use traits;
use ty::{self, Ty, TyCtxt, TypeFoldable};
use ty::subst::Substs;
use std::borrow::Cow;
use syntax::ast;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
@ -38,6 +39,25 @@ pub enum ObjectSafetyViolation {
Method(ast::Name, MethodViolationCode),
}
impl ObjectSafetyViolation {
pub fn error_msg(&self) -> Cow<'static, str> {
match *self {
ObjectSafetyViolation::SizedSelf =>
"the trait cannot require that `Self : Sized`".into(),
ObjectSafetyViolation::SupertraitSelf =>
"the trait cannot use `Self` as a type parameter \
in the supertrait listing".into(),
ObjectSafetyViolation::Method(name, MethodViolationCode::StaticMethod) =>
format!("method `{}` has no receiver", name).into(),
ObjectSafetyViolation::Method(name, MethodViolationCode::ReferencesSelf) =>
format!("method `{}` references the `Self` type \
in its arguments or return type", name).into(),
ObjectSafetyViolation::Method(name, MethodViolationCode::Generic) =>
format!("method `{}` has generic type parameters", name).into(),
}
}
}
/// Reasons a method might not be object-safe.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum MethodViolationCode {