Skip to content

Commit e52c1ec

Browse files
committed
---
yaml --- r: 172849 b: refs/heads/try c: b21a6da h: refs/heads/master i: 172847: 2d39f11 v: v3
1 parent 718de84 commit e52c1ec

File tree

20 files changed

+503
-85
lines changed

20 files changed

+503
-85
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 170c4399e614fe599c3d41306b3429ca8b3b68c6
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 5b3cd3900ceda838f5798c30ab96ceb41f962534
5-
refs/heads/try: 7b82a93be3798d9345cd459e251bcc571cf60a79
5+
refs/heads/try: b21a6da340fd958de370d2b83c0f17fd8fa51f89
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
88
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596

branches/try/src/doc/reference.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,13 @@ macro scope.
21172117
destructors from being run twice. Destructors might be run multiple times on
21182118
the same object with this attribute.
21192119
- `doc` - Doc comments such as `/// foo` are equivalent to `#[doc = "foo"]`.
2120+
- `rustc_on_unimplemented` - Write a custom note to be shown along with the error
2121+
when the trait is found to be unimplemented on a type.
2122+
You may use format arguments like `{T}`, `{A}` to correspond to the
2123+
types at the point of use corresponding to the type parameters of the
2124+
trait of the same name. `{Self}` will be replaced with the type that is supposed
2125+
to implement the trait but doesn't. To use this, the `on_unimplemented` feature gate
2126+
must be enabled.
21202127

21212128
### Conditional compilation
21222129

branches/try/src/libcore/iter.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ pub trait Iterator {
101101

102102
/// Conversion from an `Iterator`
103103
#[stable]
104+
#[rustc_on_unimplemented="a collection of type `{Self}` cannot be \
105+
built from an iterator over elements of type `{A}`"]
104106
pub trait FromIterator<A> {
105107
/// Build a container with elements from an external iterator.
106108
fn from_iter<T: Iterator<Item=A>>(iterator: T) -> Self;

branches/try/src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#![feature(simd, unsafe_destructor, slicing_syntax)]
6464
#![feature(unboxed_closures)]
6565
#![allow(unknown_features)] #![feature(int_uint)]
66+
#![feature(on_unimplemented)]
6667
#![deny(missing_docs)]
6768

6869
#[macro_use]

branches/try/src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
extern crate arena;
3434
extern crate flate;
35+
extern crate fmt_macros;
3536
extern crate getopts;
3637
extern crate graphviz;
3738
extern crate libc;

branches/try/src/librustc/lint/builtin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ impl LintPass for UnusedAttributes {
666666
"must_use",
667667
"stable",
668668
"unstable",
669+
"rustc_on_unimplemented",
669670

670671
// FIXME: #19470 this shouldn't be needed forever
671672
"old_orphan_check",

branches/try/src/librustc/middle/traits/error_reporting.rs

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ use super::{
1818
SelectionError,
1919
};
2020

21+
use fmt_macros::{Parser, Piece, Position};
2122
use middle::infer::InferCtxt;
22-
use middle::ty::{self, AsPredicate, ReferencesError, ToPolyTraitRef};
23-
use syntax::codemap::Span;
23+
use middle::ty::{self, AsPredicate, ReferencesError, ToPolyTraitRef, TraitRef};
24+
use std::collections::HashMap;
25+
use syntax::codemap::{DUMMY_SP, Span};
26+
use syntax::attr::{AttributeMethods, AttrMetaMethods};
2427
use util::ppaux::{Repr, UserString};
2528

2629
pub fn report_fulfillment_errors<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
@@ -62,6 +65,85 @@ pub fn report_projection_error<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
6265
}
6366
}
6467

68+
fn report_on_unimplemented<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
69+
trait_ref: &TraitRef<'tcx>,
70+
span: Span) -> Option<String> {
71+
let def_id = trait_ref.def_id;
72+
let mut report = None;
73+
ty::each_attr(infcx.tcx, def_id, |item| {
74+
if item.check_name("rustc_on_unimplemented") {
75+
let err_sp = if item.meta().span == DUMMY_SP {
76+
span
77+
} else {
78+
item.meta().span
79+
};
80+
let def = ty::lookup_trait_def(infcx.tcx, def_id);
81+
let trait_str = def.trait_ref.user_string(infcx.tcx);
82+
if let Some(ref istring) = item.value_str() {
83+
let mut generic_map = def.generics.types.iter_enumerated()
84+
.map(|(param, i, gen)| {
85+
(gen.name.as_str().to_string(),
86+
trait_ref.substs.types.get(param, i)
87+
.user_string(infcx.tcx))
88+
}).collect::<HashMap<String, String>>();
89+
generic_map.insert("Self".to_string(),
90+
trait_ref.self_ty().user_string(infcx.tcx));
91+
let parser = Parser::new(istring.get());
92+
let mut errored = false;
93+
let err: String = parser.filter_map(|p| {
94+
match p {
95+
Piece::String(s) => Some(s),
96+
Piece::NextArgument(a) => match a.position {
97+
Position::ArgumentNamed(s) => match generic_map.get(s) {
98+
Some(val) => Some(val.as_slice()),
99+
None => {
100+
infcx.tcx.sess
101+
.span_err(err_sp,
102+
format!("the #[rustc_on_unimplemented] \
103+
attribute on \
104+
trait definition for {} refers to \
105+
non-existent type parameter {}",
106+
trait_str, s)
107+
.as_slice());
108+
errored = true;
109+
None
110+
}
111+
},
112+
_ => {
113+
infcx.tcx.sess
114+
.span_err(err_sp,
115+
format!("the #[rustc_on_unimplemented] \
116+
attribute on \
117+
trait definition for {} must have named \
118+
format arguments, \
119+
eg `#[rustc_on_unimplemented = \
120+
\"foo {{T}}\"]`",
121+
trait_str).as_slice());
122+
errored = true;
123+
None
124+
}
125+
}
126+
}
127+
}).collect();
128+
// Report only if the format string checks out
129+
if !errored {
130+
report = Some(err);
131+
}
132+
} else {
133+
infcx.tcx.sess.span_err(err_sp,
134+
format!("the #[rustc_on_unimplemented] attribute on \
135+
trait definition for {} must have a value, \
136+
eg `#[rustc_on_unimplemented = \"foo\"]`",
137+
trait_str).as_slice());
138+
}
139+
false
140+
} else {
141+
true
142+
}
143+
});
144+
report
145+
}
146+
65147
pub fn report_selection_error<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
66148
obligation: &PredicateObligation<'tcx>,
67149
error: &SelectionError<'tcx>)
@@ -94,6 +176,14 @@ pub fn report_selection_error<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
94176
"the trait `{}` is not implemented for the type `{}`",
95177
trait_ref.user_string(infcx.tcx),
96178
trait_ref.self_ty().user_string(infcx.tcx)).as_slice());
179+
// Check if it has a custom "#[rustc_on_unimplemented]" error message,
180+
// report with that message if it does
181+
let custom_note = report_on_unimplemented(infcx, &*trait_ref.0,
182+
obligation.cause.span);
183+
if let Some(s) = custom_note {
184+
infcx.tcx.sess.span_note(obligation.cause.span,
185+
s.as_slice());
186+
}
97187
}
98188
}
99189

0 commit comments

Comments
 (0)