Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd ability to attach custom #[on_unimplemented] error messages for unimplemented traits #20889
Conversation
rust-highfive
assigned
nikomatsakis
Jan 10, 2015
This comment has been minimized.
This comment has been minimized.
|
|
Manishearth
force-pushed the
Manishearth:trait-error
branch
2 times, most recently
from
6665642
to
6f0419f
Jan 10, 2015
This comment has been minimized.
This comment has been minimized.
|
Awesome :] This should be very useful. |
Manishearth
force-pushed the
Manishearth:trait-error
branch
from
6f0419f
to
6cde8cf
Jan 10, 2015
This comment has been minimized.
This comment has been minimized.
|
Cool! |
This comment has been minimized.
This comment has been minimized.
|
looks better now, I think we should have an @nikomatsakis your call now :) |
huonw
reviewed
Jan 11, 2015
| let mut report = None; | ||
| ty::each_attr(infcx.tcx, def_id, |item| { | ||
| if item.check_name("on_unimplemented") { | ||
| match item.meta().node { |
This comment has been minimized.
This comment has been minimized.
huonw
Jan 11, 2015
Member
This pair of nested matchs can be if let Some(ref i) = item.value_str() { ... }.
huonw
reviewed
Jan 11, 2015
| if item.check_name("on_unimplemented") { | ||
| match item.meta().node { | ||
| ast::MetaItem_::MetaNameValue(_, ref lit) => match lit.node { | ||
| ast::Lit_::LitStr(ref i, _) => { |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
huonw
reviewed
Jan 11, 2015
| None => { | ||
| infcx.tcx.sess | ||
| .span_err(item.meta().span, | ||
| format!("There is no type parameter \ |
This comment has been minimized.
This comment has been minimized.
huonw
Jan 11, 2015
Member
Error messages conventionally start with a lower-case letter.
(They're printed like error: There is no type ..., and that looks a bit peculiar.)
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Yay! I wonder if we need to have a definition-site verification as well as the use-site behaviour, as it stands one will only get validation that the E.g. AFAICT, this will compile fine in isolation #[on_unimplemented = "foo {X}, {Y}, {unclosed"]
pub trait Foo {} |
Manishearth
force-pushed the
Manishearth:trait-error
branch
from
6cde8cf
to
c671926
Jan 11, 2015
This comment has been minimized.
This comment has been minimized.
|
@huonw fixed the other issues |
Manishearth
force-pushed the
Manishearth:trait-error
branch
from
c671926
to
de683c4
Jan 11, 2015
This comment has been minimized.
This comment has been minimized.
|
It's great to see this! But I think the error message should still mention the specific trait ( |
This comment has been minimized.
This comment has been minimized.
|
@P1start It is done manually, a |
This comment has been minimized.
This comment has been minimized.
|
Oh, wait, I see, you want a placeholder for the trait name. I guess that can be done manually (or I can add a |
Manishearth
force-pushed the
Manishearth:trait-error
branch
2 times, most recently
from
82295a3
to
dc0de42
Jan 11, 2015
This comment has been minimized.
This comment has been minimized.
|
@huon I made it a note, and added a lint/test. |
huonw
reviewed
Jan 11, 2015
| @@ -88,12 +154,20 @@ pub fn report_selection_error<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>, | |||
| infcx.resolve_type_vars_if_possible(trait_predicate); | |||
| if !trait_predicate.references_error() { | |||
| let trait_ref = trait_predicate.to_poly_trait_ref(); | |||
| // Check if it has a custom "#[on_unimplemented]" error message, | |||
| // report with that message if it does | |||
| let custom_note = report_on_unimplemented(infcx, &*trait_ref.0); | |||
This comment has been minimized.
This comment has been minimized.
huonw
Jan 11, 2015
Member
This is probably best run after the main error message, just in case the span_errs inside it are triggered, so that the output has a slightly more sensible order.
huonw
reviewed
Jan 11, 2015
| Some(val) => Some(val.as_slice()), | ||
| None => { | ||
| infcx.tcx.sess | ||
| .span_err(item.meta().span, |
This comment has been minimized.
This comment has been minimized.
huonw
Jan 11, 2015
Member
Hm, I suspect these spans are useless for the most part (I imagine they are usually cross-crate, and so unable to point to the actual attribute in question). It may be better to either use obligation.cause.span always, or detect if item.meta().span is syntax::codemap::DUMMY_SP or not and default to obligation.cause.span when it is DUMMY_SP.
This comment has been minimized.
This comment has been minimized.
huonw
Jan 11, 2015
Member
(In fact, thinking about it now, it may also make sense to make these error messages clearer that they are talking about the on_unimplemented message, not the code you're calling, something like invalid #[on_unimplemented] format: there is no....)
Manishearth
added some commits
Jan 11, 2015
Ms2ger
reviewed
Jan 11, 2015
| let custom_note = report_on_unimplemented(infcx, &*trait_ref.0, | ||
| obligation.cause.span); | ||
| if let Some(s) = custom_note { | ||
| infcx.tcx.sess.span_note( |
This comment has been minimized.
This comment has been minimized.
Manishearth
force-pushed the
Manishearth:trait-error
branch
from
f0e6e8d
to
ad7e33e
Jan 11, 2015
nikomatsakis
reviewed
Jan 11, 2015
|
|
||
| #[rustc_on_unimplemented = "Unimplemented trait error on `{Self}` with params `<{A},{B},{C}>`"] | ||
| //~^ ERROR there is no type parameter C on trait BadAnnotation2 | ||
| trait BadAnnotation2<A,B> {} |
This comment has been minimized.
This comment has been minimized.
Manishearth
force-pushed the
Manishearth:trait-error
branch
2 times, most recently
from
2f5767f
to
02d0a8b
Jan 11, 2015
This comment has been minimized.
This comment has been minimized.
nrc
commented on 02d0a8b
Jan 12, 2015
|
r=nikomatsakis |
This comment has been minimized.
This comment has been minimized.
|
saw approval from nikomatsakis |
This comment has been minimized.
This comment has been minimized.
|
merging Manishearth/rust/trait-error = 02d0a8b into auto |
This comment has been minimized.
This comment has been minimized.
|
status: {"merge_sha": "0aec4db1c09574da2f30e3844de6d252d79d4939"} |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
fast-forwarding master to auto = 0aec4db |
This comment has been minimized.
This comment has been minimized.
|
fast-forwarding master to auto = 0aec4db |
Manishearth commentedJan 10, 2015
fixes #20783
r? @nikomatsakis