1 use fmt_macros::{Parser, Piece, Position};
3 use crate::hir::def_id::DefId;
4 use crate::ty::{self, TyCtxt, GenericParamDefKind};
5 use crate::util::common::ErrorReported;
6 use crate::util::nodemap::FxHashMap;
8 use syntax::ast::{MetaItem, NestedMetaItem};
10 use syntax::symbol::{Symbol, kw, sym};
13 #[derive(Clone, Debug)]
14 pub struct OnUnimplementedFormatString(Symbol);
17 pub struct OnUnimplementedDirective {
18 pub condition: Option<MetaItem>,
19 pub subcommands: Vec<OnUnimplementedDirective>,
20 pub message: Option<OnUnimplementedFormatString>,
21 pub label: Option<OnUnimplementedFormatString>,
22 pub note: Option<OnUnimplementedFormatString>,
25 pub struct OnUnimplementedNote {
26 pub message: Option<String>,
27 pub label: Option<String>,
28 pub note: Option<String>,
31 impl OnUnimplementedNote {
32 pub fn empty() -> Self {
33 OnUnimplementedNote { message: None, label: None, note: None }
44 let mut diag = struct_span_err!(
45 tcx.sess, span, E0232, "{}", message);
46 diag.span_label(span, label);
47 if let Some(note) = note {
54 impl<'tcx> OnUnimplementedDirective {
58 items: &[NestedMetaItem],
61 ) -> Result<Self, ErrorReported> {
62 let mut errored = false;
63 let mut item_iter = items.iter();
65 let condition = if is_root {
68 let cond = item_iter.next().ok_or_else(||
69 parse_error(tcx, span,
70 "empty `on`-clause in `#[rustc_on_unimplemented]`",
71 "empty on-clause here",
73 )?.meta_item().ok_or_else(||
74 parse_error(tcx, span,
75 "invalid `on`-clause in `#[rustc_on_unimplemented]`",
76 "invalid on-clause here",
79 attr::eval_condition(cond, &tcx.sess.parse_sess, &mut |_| true);
83 let mut message = None;
86 let mut subcommands = vec![];
87 for item in item_iter {
88 if item.check_name(sym::message) && message.is_none() {
89 if let Some(message_) = item.value_str() {
90 message = Some(OnUnimplementedFormatString::try_parse(
91 tcx, trait_def_id, message_, span)?);
94 } else if item.check_name(sym::label) && label.is_none() {
95 if let Some(label_) = item.value_str() {
96 label = Some(OnUnimplementedFormatString::try_parse(
97 tcx, trait_def_id, label_, span)?);
100 } else if item.check_name(sym::note) && note.is_none() {
101 if let Some(note_) = item.value_str() {
102 note = Some(OnUnimplementedFormatString::try_parse(
103 tcx, trait_def_id, note_, span)?);
106 } else if item.check_name(sym::on) && is_root &&
107 message.is_none() && label.is_none() && note.is_none()
109 if let Some(items) = item.meta_item_list() {
110 if let Ok(subcommand) =
111 Self::parse(tcx, trait_def_id, &items, item.span(), false)
113 subcommands.push(subcommand);
122 parse_error(tcx, item.span(),
123 "this attribute must have a valid value",
124 "expected value here",
125 Some(r#"eg `#[rustc_on_unimplemented(message="foo")]`"#));
131 Ok(OnUnimplementedDirective { condition, message, label, subcommands, note })
139 ) -> Result<Option<Self>, ErrorReported> {
140 let attrs = tcx.get_attrs(impl_def_id);
142 let attr = if let Some(item) = attr::find_by_name(&attrs, sym::rustc_on_unimplemented) {
148 let result = if let Some(items) = attr.meta_item_list() {
149 Self::parse(tcx, trait_def_id, &items, attr.span, true).map(Some)
150 } else if let Some(value) = attr.value_str() {
151 Ok(Some(OnUnimplementedDirective {
155 label: Some(OnUnimplementedFormatString::try_parse(
156 tcx, trait_def_id, value, attr.span)?),
160 return Err(ErrorReported);
162 debug!("of_item({:?}/{:?}) = {:?}", trait_def_id, impl_def_id, result);
169 trait_ref: ty::TraitRef<'tcx>,
170 options: &[(Symbol, Option<String>)],
171 ) -> OnUnimplementedNote {
172 let mut message = None;
173 let mut label = None;
175 info!("evaluate({:?}, trait_ref={:?}, options={:?})", self, trait_ref, options);
177 for command in self.subcommands.iter().chain(Some(self)).rev() {
178 if let Some(ref condition) = command.condition {
179 if !attr::eval_condition(condition, &tcx.sess.parse_sess, &mut |c| {
180 c.ident().map_or(false, |ident| {
183 c.value_str().map(|s| s.as_str().to_string())
187 debug!("evaluate: skipping {:?} due to condition", command);
191 debug!("evaluate: {:?} succeeded", command);
192 if let Some(ref message_) = command.message {
193 message = Some(message_.clone());
196 if let Some(ref label_) = command.label {
197 label = Some(label_.clone());
200 if let Some(ref note_) = command.note {
201 note = Some(note_.clone());
205 let options: FxHashMap<Symbol, String> = options.into_iter()
206 .filter_map(|(k, v)| v.as_ref().map(|v| (*k, v.to_owned())))
208 OnUnimplementedNote {
209 label: label.map(|l| l.format(tcx, trait_ref, &options)),
210 message: message.map(|m| m.format(tcx, trait_ref, &options)),
211 note: note.map(|n| n.format(tcx, trait_ref, &options)),
216 impl<'tcx> OnUnimplementedFormatString {
222 ) -> Result<Self, ErrorReported> {
223 let result = OnUnimplementedFormatString(from);
224 result.verify(tcx, trait_def_id, err_sp)?;
233 ) -> Result<(), ErrorReported> {
234 let name = tcx.item_name(trait_def_id);
235 let generics = tcx.generics_of(trait_def_id);
236 let s = self.0.as_str();
237 let parser = Parser::new(&s, None, vec![], false);
238 let mut result = Ok(());
239 for token in parser {
241 Piece::String(_) => (), // Normal string, no need to check it
242 Piece::NextArgument(a) => match a.position {
243 // `{Self}` is allowed
244 Position::ArgumentNamed(s) if s == kw::SelfUpper => (),
245 // `{ThisTraitsName}` is allowed
246 Position::ArgumentNamed(s) if s == name => (),
247 // `{from_method}` is allowed
248 Position::ArgumentNamed(s) if s == sym::from_method => (),
249 // `{from_desugaring}` is allowed
250 Position::ArgumentNamed(s) if s == sym::from_desugaring => (),
251 // So is `{A}` if A is a type parameter
252 Position::ArgumentNamed(s) => match generics.params.iter().find(|param| {
257 span_err!(tcx.sess, span, E0230,
258 "there is no parameter `{}` on trait `{}`", s, name);
259 result = Err(ErrorReported);
262 // `{:1}` and `{}` are not to be used
263 Position::ArgumentIs(_) | Position::ArgumentImplicitlyIs(_) => {
264 span_err!(tcx.sess, span, E0231,
265 "only named substitution parameters are allowed");
266 result = Err(ErrorReported);
278 trait_ref: ty::TraitRef<'tcx>,
279 options: &FxHashMap<Symbol, String>,
281 let name = tcx.item_name(trait_ref.def_id);
282 let trait_str = tcx.def_path_str(trait_ref.def_id);
283 let generics = tcx.generics_of(trait_ref.def_id);
284 let generic_map = generics.params.iter().filter_map(|param| {
285 let value = match param.kind {
286 GenericParamDefKind::Type { .. } |
287 GenericParamDefKind::Const => {
288 trait_ref.substs[param.index as usize].to_string()
290 GenericParamDefKind::Lifetime => return None
292 let name = param.name;
294 }).collect::<FxHashMap<Symbol, String>>();
295 let empty_string = String::new();
297 let s = self.0.as_str();
298 let parser = Parser::new(&s, None, vec![], false);
301 Piece::String(s) => s,
302 Piece::NextArgument(a) => match a.position {
303 Position::ArgumentNamed(s) => match generic_map.get(&s) {
305 None if s == name => {
309 if let Some(val) = options.get(&s) {
311 } else if s == sym::from_desugaring || s == sym::from_method {
312 // don't break messages using these two arguments incorrectly
315 bug!("broken on_unimplemented {:?} for {:?}: \
316 no argument matching {:?}",
317 self.0, trait_ref, s)
321 _ => bug!("broken on_unimplemented {:?} - bad format arg", self.0)