|
| 1 | +use rustc_feature::template; |
| 2 | +use rustc_hir::attrs::AttributeKind; |
| 3 | +use rustc_hir::lints::AttributeLintKind; |
| 4 | +use rustc_session::lint::builtin::MALFORMED_DIAGNOSTIC_ATTRIBUTES; |
| 5 | +use rustc_span::sym; |
| 6 | + |
| 7 | +use crate::attributes::diagnostic::*; |
| 8 | +use crate::attributes::prelude::*; |
| 9 | +use crate::context::{AcceptContext, Stage}; |
| 10 | +use crate::parser::ArgParser; |
| 11 | +use crate::target_checking::{ALL_TARGETS, AllowedTargets}; |
| 12 | + |
| 13 | +#[derive(Default)] |
| 14 | +pub(crate) struct OnTypeErrorParser { |
| 15 | + span: Option<Span>, |
| 16 | + directive: Option<(Span, Directive)>, |
| 17 | +} |
| 18 | + |
| 19 | +impl OnTypeErrorParser { |
| 20 | + fn parse<'sess, S: Stage>( |
| 21 | + &mut self, |
| 22 | + cx: &mut AcceptContext<'_, 'sess, S>, |
| 23 | + args: &ArgParser, |
| 24 | + mode: Mode, |
| 25 | + ) { |
| 26 | + if !cx.features().diagnostic_on_type_error() { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + let span = cx.attr_span; |
| 31 | + self.span = Some(span); |
| 32 | + |
| 33 | + let Some(list) = args.list() else { |
| 34 | + cx.emit_lint( |
| 35 | + MALFORMED_DIAGNOSTIC_ATTRIBUTES, |
| 36 | + AttributeLintKind::MissingOptionsForOnTypeError, |
| 37 | + span, |
| 38 | + ); |
| 39 | + return; |
| 40 | + }; |
| 41 | + |
| 42 | + if list.is_empty() { |
| 43 | + cx.emit_lint( |
| 44 | + MALFORMED_DIAGNOSTIC_ATTRIBUTES, |
| 45 | + AttributeLintKind::OnTypeErrorMalformedAttrExpectedLiteralOrDelimiter, |
| 46 | + list.span, |
| 47 | + ); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if let Some(directive) = parse_directive_items(cx, mode, list.mixed(), true) { |
| 52 | + merge_directives(cx, &mut self.directive, (span, directive)); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl<S: Stage> AttributeParser<S> for OnTypeErrorParser { |
| 58 | + const ATTRIBUTES: AcceptMapping<Self, S> = &[( |
| 59 | + &[sym::diagnostic, sym::on_type_error], |
| 60 | + template!(List: &[r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#]), |
| 61 | + |this, cx, args| { |
| 62 | + this.parse(cx, args, Mode::DiagnosticOnTypeError); |
| 63 | + }, |
| 64 | + )]; |
| 65 | + |
| 66 | + const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); |
| 67 | + |
| 68 | + fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> { |
| 69 | + if let Some(span) = self.span { |
| 70 | + Some(AttributeKind::OnTypeError { |
| 71 | + span, |
| 72 | + directive: self.directive.map(|d| Box::new(d.1)), |
| 73 | + }) |
| 74 | + } else { |
| 75 | + None |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments