Skip to content

Commit 45aadf7

Browse files
committed
add enclosing_scope param to rustc_on_unimplmented
add ui test compute enclosing_scope_span on demand add scope test make tidy happy stylistic and typo fixes
1 parent 582a4ea commit 45aadf7

File tree

5 files changed

+143
-14
lines changed

5 files changed

+143
-14
lines changed

src/librustc/traits/error_reporting.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
519519
) {
520520
command.evaluate(self.tcx, trait_ref, &flags[..])
521521
} else {
522-
OnUnimplementedNote::empty()
522+
OnUnimplementedNote::default()
523523
}
524524
}
525525

@@ -695,6 +695,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
695695
fallback_has_occurred: bool,
696696
points_at_arg: bool,
697697
) {
698+
let tcx = self.tcx;
698699
let span = obligation.cause.span;
699700

700701
let mut err = match *error {
@@ -730,6 +731,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
730731
message,
731732
label,
732733
note,
734+
enclosing_scope,
733735
} = self.on_unimplemented_note(trait_ref, obligation);
734736
let have_alt_message = message.is_some() || label.is_some();
735737
let is_try = self.tcx.sess.source_map().span_to_snippet(span)
@@ -794,6 +796,19 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
794796
// If it has a custom `#[rustc_on_unimplemented]` note, let's display it
795797
err.note(s.as_str());
796798
}
799+
if let Some(ref s) = enclosing_scope {
800+
let enclosing_scope_span = tcx.def_span(
801+
tcx.hir()
802+
.opt_local_def_id(obligation.cause.body_id)
803+
.unwrap_or_else(|| {
804+
tcx.hir().body_owner_def_id(hir::BodyId {
805+
hir_id: obligation.cause.body_id,
806+
})
807+
}),
808+
);
809+
810+
err.span_label(enclosing_scope_span, s.as_str());
811+
}
797812

798813
self.suggest_borrow_on_unsized_slice(&obligation.cause.code, &mut err);
799814
self.suggest_fn_call(&obligation, &mut err, &trait_ref, points_at_arg);

src/librustc/traits/on_unimplemented.rs

+33-13
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@ pub struct OnUnimplementedDirective {
2222
pub message: Option<OnUnimplementedFormatString>,
2323
pub label: Option<OnUnimplementedFormatString>,
2424
pub note: Option<OnUnimplementedFormatString>,
25+
pub enclosing_scope: Option<OnUnimplementedFormatString>,
2526
}
2627

28+
#[derive(Default)]
2729
pub struct OnUnimplementedNote {
2830
pub message: Option<String>,
2931
pub label: Option<String>,
3032
pub note: Option<String>,
31-
}
32-
33-
impl OnUnimplementedNote {
34-
pub fn empty() -> Self {
35-
OnUnimplementedNote { message: None, label: None, note: None }
36-
}
33+
pub enclosing_scope: Option<String>,
3734
}
3835

3936
fn parse_error(
@@ -85,24 +82,33 @@ impl<'tcx> OnUnimplementedDirective {
8582
let mut message = None;
8683
let mut label = None;
8784
let mut note = None;
85+
let mut enclosing_scope = None;
8886
let mut subcommands = vec![];
87+
88+
let parse_value = |value_str| {
89+
OnUnimplementedFormatString::try_parse(tcx, trait_def_id, value_str, span)
90+
.map(Some)
91+
};
92+
8993
for item in item_iter {
9094
if item.check_name(sym::message) && message.is_none() {
9195
if let Some(message_) = item.value_str() {
92-
message = Some(OnUnimplementedFormatString::try_parse(
93-
tcx, trait_def_id, message_, span)?);
96+
message = parse_value(message_)?;
9497
continue;
9598
}
9699
} else if item.check_name(sym::label) && label.is_none() {
97100
if let Some(label_) = item.value_str() {
98-
label = Some(OnUnimplementedFormatString::try_parse(
99-
tcx, trait_def_id, label_, span)?);
101+
label = parse_value(label_)?;
100102
continue;
101103
}
102104
} else if item.check_name(sym::note) && note.is_none() {
103105
if let Some(note_) = item.value_str() {
104-
note = Some(OnUnimplementedFormatString::try_parse(
105-
tcx, trait_def_id, note_, span)?);
106+
note = parse_value(note_)?;
107+
continue;
108+
}
109+
} else if item.check_name(sym::enclosing_scope) && enclosing_scope.is_none() {
110+
if let Some(enclosing_scope_) = item.value_str() {
111+
enclosing_scope = parse_value(enclosing_scope_)?;
106112
continue;
107113
}
108114
} else if item.check_name(sym::on) && is_root &&
@@ -130,7 +136,14 @@ impl<'tcx> OnUnimplementedDirective {
130136
if errored {
131137
Err(ErrorReported)
132138
} else {
133-
Ok(OnUnimplementedDirective { condition, message, label, subcommands, note })
139+
Ok(OnUnimplementedDirective {
140+
condition,
141+
subcommands,
142+
message,
143+
label,
144+
note,
145+
enclosing_scope
146+
})
134147
}
135148
}
136149

@@ -157,6 +170,7 @@ impl<'tcx> OnUnimplementedDirective {
157170
label: Some(OnUnimplementedFormatString::try_parse(
158171
tcx, trait_def_id, value, attr.span)?),
159172
note: None,
173+
enclosing_scope: None,
160174
}))
161175
} else {
162176
return Err(ErrorReported);
@@ -174,6 +188,7 @@ impl<'tcx> OnUnimplementedDirective {
174188
let mut message = None;
175189
let mut label = None;
176190
let mut note = None;
191+
let mut enclosing_scope = None;
177192
info!("evaluate({:?}, trait_ref={:?}, options={:?})", self, trait_ref, options);
178193

179194
for command in self.subcommands.iter().chain(Some(self)).rev() {
@@ -202,6 +217,10 @@ impl<'tcx> OnUnimplementedDirective {
202217
if let Some(ref note_) = command.note {
203218
note = Some(note_.clone());
204219
}
220+
221+
if let Some(ref enclosing_scope_) = command.enclosing_scope {
222+
enclosing_scope = Some(enclosing_scope_.clone());
223+
}
205224
}
206225

207226
let options: FxHashMap<Symbol, String> = options.into_iter()
@@ -211,6 +230,7 @@ impl<'tcx> OnUnimplementedDirective {
211230
label: label.map(|l| l.format(tcx, trait_ref, &options)),
212231
message: message.map(|m| m.format(tcx, trait_ref, &options)),
213232
note: note.map(|n| n.format(tcx, trait_ref, &options)),
233+
enclosing_scope: enclosing_scope.map(|e_s| e_s.format(tcx, trait_ref, &options)),
214234
}
215235
}
216236
}

src/libsyntax_pos/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ symbols! {
279279
Err,
280280
Eq,
281281
Equal,
282+
enclosing_scope,
282283
except,
283284
exclusive_range_pattern,
284285
exhaustive_integer_patterns,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Test scope annotations from `enclosing_scope` parameter
2+
3+
#![feature(rustc_attrs)]
4+
5+
#[rustc_on_unimplemented(enclosing_scope="in this scope")]
6+
trait Trait{}
7+
8+
struct Foo;
9+
10+
fn f<T: Trait>(x: T) {}
11+
12+
fn main() {
13+
let x = || {
14+
f(Foo{}); //~ ERROR the trait bound `Foo: Trait` is not satisfied
15+
let y = || {
16+
f(Foo{}); //~ ERROR the trait bound `Foo: Trait` is not satisfied
17+
};
18+
};
19+
20+
{
21+
{
22+
f(Foo{}); //~ ERROR the trait bound `Foo: Trait` is not satisfied
23+
}
24+
}
25+
26+
f(Foo{}); //~ ERROR the trait bound `Foo: Trait` is not satisfied
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
error[E0277]: the trait bound `Foo: Trait` is not satisfied
2+
--> $DIR/enclosing-scope.rs:14:11
3+
|
4+
LL | fn f<T: Trait>(x: T) {}
5+
| - ----- required by this bound in `f`
6+
...
7+
LL | let x = || {
8+
| _____________-
9+
LL | | f(Foo{});
10+
| | ^^^^^ the trait `Trait` is not implemented for `Foo`
11+
LL | | let y = || {
12+
LL | | f(Foo{});
13+
LL | | };
14+
LL | | };
15+
| |_____- in this scope
16+
17+
error[E0277]: the trait bound `Foo: Trait` is not satisfied
18+
--> $DIR/enclosing-scope.rs:16:15
19+
|
20+
LL | fn f<T: Trait>(x: T) {}
21+
| - ----- required by this bound in `f`
22+
...
23+
LL | let y = || {
24+
| _________________-
25+
LL | | f(Foo{});
26+
| | ^^^^^ the trait `Trait` is not implemented for `Foo`
27+
LL | | };
28+
| |_________- in this scope
29+
30+
error[E0277]: the trait bound `Foo: Trait` is not satisfied
31+
--> $DIR/enclosing-scope.rs:22:15
32+
|
33+
LL | fn f<T: Trait>(x: T) {}
34+
| - ----- required by this bound in `f`
35+
LL |
36+
LL | / fn main() {
37+
LL | | let x = || {
38+
LL | | f(Foo{});
39+
LL | | let y = || {
40+
... |
41+
LL | | f(Foo{});
42+
| | ^^^^^ the trait `Trait` is not implemented for `Foo`
43+
... |
44+
LL | | f(Foo{});
45+
LL | | }
46+
| |_- in this scope
47+
48+
error[E0277]: the trait bound `Foo: Trait` is not satisfied
49+
--> $DIR/enclosing-scope.rs:26:7
50+
|
51+
LL | fn f<T: Trait>(x: T) {}
52+
| - ----- required by this bound in `f`
53+
LL |
54+
LL | / fn main() {
55+
LL | | let x = || {
56+
LL | | f(Foo{});
57+
LL | | let y = || {
58+
... |
59+
LL | | f(Foo{});
60+
| | ^^^^^ the trait `Trait` is not implemented for `Foo`
61+
LL | | }
62+
| |_- in this scope
63+
64+
error: aborting due to 4 previous errors
65+
66+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)