Skip to content

Commit 2436d73

Browse files
committed
Extract out error message generation.
1 parent a97aed7 commit 2436d73

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

src/librustc/traits/error_reporting.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use super::{
2121
SelectionContext,
2222
SelectionError,
2323
ObjectSafetyViolation,
24-
MethodViolationCode,
2524
};
2625

2726
use fmt_macros::{Parser, Piece, Position};
@@ -679,38 +678,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
679678
if !reported_violations.insert(violation.clone()) {
680679
continue;
681680
}
682-
let buf;
683-
let note = match violation {
684-
ObjectSafetyViolation::SizedSelf => {
685-
"the trait cannot require that `Self : Sized`"
686-
}
687-
688-
ObjectSafetyViolation::SupertraitSelf => {
689-
"the trait cannot use `Self` as a type parameter \
690-
in the supertrait listing"
691-
}
692-
693-
ObjectSafetyViolation::Method(name,
694-
MethodViolationCode::StaticMethod) => {
695-
buf = format!("method `{}` has no receiver", name);
696-
&buf
697-
}
698-
699-
ObjectSafetyViolation::Method(name,
700-
MethodViolationCode::ReferencesSelf) => {
701-
buf = format!("method `{}` references the `Self` type \
702-
in its arguments or return type",
703-
name);
704-
&buf
705-
}
706-
707-
ObjectSafetyViolation::Method(name,
708-
MethodViolationCode::Generic) => {
709-
buf = format!("method `{}` has generic type parameters", name);
710-
&buf
711-
}
712-
};
713-
err.note(note);
681+
err.note(&violation.error_msg());
714682
}
715683
err
716684
}

src/librustc/traits/object_safety.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use hir::def_id::DefId;
2323
use traits;
2424
use ty::{self, Ty, TyCtxt, TypeFoldable};
2525
use ty::subst::Substs;
26+
use std::borrow::Cow;
2627
use syntax::ast;
2728

2829
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
@@ -38,6 +39,25 @@ pub enum ObjectSafetyViolation {
3839
Method(ast::Name, MethodViolationCode),
3940
}
4041

42+
impl ObjectSafetyViolation {
43+
pub fn error_msg(&self) -> Cow<'static, str> {
44+
match *self {
45+
ObjectSafetyViolation::SizedSelf =>
46+
"the trait cannot require that `Self : Sized`".into(),
47+
ObjectSafetyViolation::SupertraitSelf =>
48+
"the trait cannot use `Self` as a type parameter \
49+
in the supertrait listing".into(),
50+
ObjectSafetyViolation::Method(name, MethodViolationCode::StaticMethod) =>
51+
format!("method `{}` has no receiver", name).into(),
52+
ObjectSafetyViolation::Method(name, MethodViolationCode::ReferencesSelf) =>
53+
format!("method `{}` references the `Self` type \
54+
in its arguments or return type", name).into(),
55+
ObjectSafetyViolation::Method(name, MethodViolationCode::Generic) =>
56+
format!("method `{}` has generic type parameters", name).into(),
57+
}
58+
}
59+
}
60+
4161
/// Reasons a method might not be object-safe.
4262
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
4363
pub enum MethodViolationCode {

0 commit comments

Comments
 (0)