1 use rustc_ast::{MetaItem, NestedMetaItem};
2 use rustc_attr as attr;
3 use rustc_data_structures::fx::FxHashMap;
4 use rustc_errors::{struct_span_err, ErrorGuaranteed};
5 use rustc_hir::def_id::DefId;
6 use rustc_middle::ty::{self, GenericParamDefKind, TyCtxt};
7 use rustc_parse_format::{ParseMode, Parser, Piece, Position};
8 use rustc_span::symbol::{kw, sym, Symbol};
9 use rustc_span::{Span, DUMMY_SP};
11 #[derive(Clone, Debug)]
12 pub struct OnUnimplementedFormatString(Symbol);
15 pub struct OnUnimplementedDirective {
16 pub condition: Option<MetaItem>,
17 pub subcommands: Vec<OnUnimplementedDirective>,
18 pub message: Option<OnUnimplementedFormatString>,
19 pub label: Option<OnUnimplementedFormatString>,
20 pub note: Option<OnUnimplementedFormatString>,
21 pub enclosing_scope: Option<OnUnimplementedFormatString>,
22 pub append_const_msg: Option<Option<Symbol>>,
26 pub struct OnUnimplementedNote {
27 pub message: Option<String>,
28 pub label: Option<String>,
29 pub note: Option<String>,
30 pub enclosing_scope: Option<String>,
31 /// Append a message for `~const Trait` errors. `None` means not requested and
32 /// should fallback to a generic message, `Some(None)` suggests using the default
33 /// appended message, `Some(Some(s))` suggests use the `s` message instead of the
35 pub append_const_msg: Option<Option<Symbol>>,
44 ) -> ErrorGuaranteed {
45 let mut diag = struct_span_err!(tcx.sess, span, E0232, "{}", message);
46 diag.span_label(span, label);
47 if let Some(note) = note {
53 impl<'tcx> OnUnimplementedDirective {
57 items: &[NestedMetaItem],
60 ) -> Result<Self, ErrorGuaranteed> {
61 let mut errored = None;
62 let mut item_iter = items.iter();
64 let parse_value = |value_str| {
65 OnUnimplementedFormatString::try_parse(tcx, item_def_id, value_str, span).map(Some)
68 let condition = if is_root {
77 "empty `on`-clause in `#[rustc_on_unimplemented]`",
78 "empty on-clause here",
87 "invalid `on`-clause in `#[rustc_on_unimplemented]`",
88 "invalid on-clause here",
92 attr::eval_condition(cond, &tcx.sess.parse_sess, Some(tcx.features()), &mut |cfg| {
93 if let Some(value) = cfg.value && let Err(guar) = parse_value(value) {
101 let mut message = None;
102 let mut label = None;
104 let mut enclosing_scope = None;
105 let mut subcommands = vec![];
106 let mut append_const_msg = None;
108 for item in item_iter {
109 if item.has_name(sym::message) && message.is_none() {
110 if let Some(message_) = item.value_str() {
111 message = parse_value(message_)?;
114 } else if item.has_name(sym::label) && label.is_none() {
115 if let Some(label_) = item.value_str() {
116 label = parse_value(label_)?;
119 } else if item.has_name(sym::note) && note.is_none() {
120 if let Some(note_) = item.value_str() {
121 note = parse_value(note_)?;
124 } else if item.has_name(sym::enclosing_scope) && enclosing_scope.is_none() {
125 if let Some(enclosing_scope_) = item.value_str() {
126 enclosing_scope = parse_value(enclosing_scope_)?;
129 } else if item.has_name(sym::on)
135 if let Some(items) = item.meta_item_list() {
136 match Self::parse(tcx, item_def_id, &items, item.span(), false) {
137 Ok(subcommand) => subcommands.push(subcommand),
138 Err(reported) => errored = Some(reported),
142 } else if item.has_name(sym::append_const_msg) && append_const_msg.is_none() {
143 if let Some(msg) = item.value_str() {
144 append_const_msg = Some(Some(msg));
146 } else if item.is_word() {
147 append_const_msg = Some(None);
156 "this attribute must have a valid value",
157 "expected value here",
158 Some(r#"eg `#[rustc_on_unimplemented(message="foo")]`"#),
162 if let Some(reported) = errored {
165 Ok(OnUnimplementedDirective {
177 pub fn of_item(tcx: TyCtxt<'tcx>, item_def_id: DefId) -> Result<Option<Self>, ErrorGuaranteed> {
178 let Some(attr) = tcx.get_attr(item_def_id, sym::rustc_on_unimplemented) else {
182 let result = if let Some(items) = attr.meta_item_list() {
183 Self::parse(tcx, item_def_id, &items, attr.span, true).map(Some)
184 } else if let Some(value) = attr.value_str() {
185 Ok(Some(OnUnimplementedDirective {
189 label: Some(OnUnimplementedFormatString::try_parse(
196 enclosing_scope: None,
197 append_const_msg: None,
201 tcx.sess.delay_span_bug(DUMMY_SP, "of_item: neither meta_item_list nor value_str");
202 return Err(reported);
204 debug!("of_item({:?}) = {:?}", item_def_id, result);
211 trait_ref: ty::TraitRef<'tcx>,
212 options: &[(Symbol, Option<String>)],
213 ) -> OnUnimplementedNote {
214 let mut message = None;
215 let mut label = None;
217 let mut enclosing_scope = None;
218 let mut append_const_msg = None;
219 info!("evaluate({:?}, trait_ref={:?}, options={:?})", self, trait_ref, options);
221 let options_map: FxHashMap<Symbol, String> =
222 options.iter().filter_map(|(k, v)| v.as_ref().map(|v| (*k, v.to_owned()))).collect();
224 for command in self.subcommands.iter().chain(Some(self)).rev() {
225 if let Some(ref condition) = command.condition && !attr::eval_condition(
227 &tcx.sess.parse_sess,
228 Some(tcx.features()),
230 let value = cfg.value.map(|v| {
231 OnUnimplementedFormatString(v).format(tcx, trait_ref, &options_map)
234 options.contains(&(cfg.name, value))
237 debug!("evaluate: skipping {:?} due to condition", command);
240 debug!("evaluate: {:?} succeeded", command);
241 if let Some(ref message_) = command.message {
242 message = Some(message_.clone());
245 if let Some(ref label_) = command.label {
246 label = Some(label_.clone());
249 if let Some(ref note_) = command.note {
250 note = Some(note_.clone());
253 if let Some(ref enclosing_scope_) = command.enclosing_scope {
254 enclosing_scope = Some(enclosing_scope_.clone());
257 append_const_msg = command.append_const_msg;
260 OnUnimplementedNote {
261 label: label.map(|l| l.format(tcx, trait_ref, &options_map)),
262 message: message.map(|m| m.format(tcx, trait_ref, &options_map)),
263 note: note.map(|n| n.format(tcx, trait_ref, &options_map)),
264 enclosing_scope: enclosing_scope.map(|e_s| e_s.format(tcx, trait_ref, &options_map)),
270 impl<'tcx> OnUnimplementedFormatString {
276 ) -> Result<Self, ErrorGuaranteed> {
277 let result = OnUnimplementedFormatString(from);
278 result.verify(tcx, item_def_id, err_sp)?;
287 ) -> Result<(), ErrorGuaranteed> {
288 let trait_def_id = if tcx.is_trait(item_def_id) {
291 tcx.trait_id_of_impl(item_def_id)
292 .expect("expected `on_unimplemented` to correspond to a trait")
294 let trait_name = tcx.item_name(trait_def_id);
295 let generics = tcx.generics_of(item_def_id);
296 let s = self.0.as_str();
297 let parser = Parser::new(s, None, None, false, ParseMode::Format);
298 let mut result = Ok(());
299 for token in parser {
301 Piece::String(_) => (), // Normal string, no need to check it
302 Piece::NextArgument(a) => match a.position {
303 Position::ArgumentNamed(s) => {
304 match Symbol::intern(s) {
305 // `{Self}` is allowed
307 // `{ThisTraitsName}` is allowed
308 s if s == trait_name => (),
309 // `{from_method}` is allowed
310 sym::from_method => (),
311 // `{from_desugaring}` is allowed
312 sym::from_desugaring => (),
313 // `{ItemContext}` is allowed
314 sym::ItemContext => (),
315 // `{integral}` and `{integer}` and `{float}` are allowed
316 sym::integral | sym::integer_ | sym::float => (),
317 // So is `{A}` if A is a type parameter
318 s => match generics.params.iter().find(|param| param.name == s) {
321 let reported = struct_span_err!(
325 "there is no parameter `{}` on {}",
327 if trait_def_id == item_def_id {
328 format!("trait `{}`", trait_name)
334 result = Err(reported);
339 // `{:1}` and `{}` are not to be used
340 Position::ArgumentIs(..) | Position::ArgumentImplicitlyIs(_) => {
341 let reported = struct_span_err!(
345 "only named substitution parameters are allowed"
348 result = Err(reported);
360 trait_ref: ty::TraitRef<'tcx>,
361 options: &FxHashMap<Symbol, String>,
363 let name = tcx.item_name(trait_ref.def_id);
364 let trait_str = tcx.def_path_str(trait_ref.def_id);
365 let generics = tcx.generics_of(trait_ref.def_id);
366 let generic_map = generics
369 .filter_map(|param| {
370 let value = match param.kind {
371 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
372 trait_ref.substs[param.index as usize].to_string()
374 GenericParamDefKind::Lifetime => return None,
376 let name = param.name;
379 .collect::<FxHashMap<Symbol, String>>();
380 let empty_string = String::new();
382 let s = self.0.as_str();
383 let parser = Parser::new(s, None, None, false, ParseMode::Format);
384 let item_context = (options.get(&sym::ItemContext)).unwrap_or(&empty_string);
387 Piece::String(s) => s,
388 Piece::NextArgument(a) => match a.position {
389 Position::ArgumentNamed(s) => {
390 let s = Symbol::intern(s);
391 match generic_map.get(&s) {
393 None if s == name => &trait_str,
395 if let Some(val) = options.get(&s) {
397 } else if s == sym::from_desugaring || s == sym::from_method {
398 // don't break messages using these two arguments incorrectly
400 } else if s == sym::ItemContext {
402 } else if s == sym::integral {
404 } else if s == sym::integer_ {
406 } else if s == sym::float {
410 "broken on_unimplemented {:?} for {:?}: \
411 no argument matching {:?}",
420 _ => bug!("broken on_unimplemented {:?} - bad format arg", self.0),