Skip to content

Commit 25a6fca

Browse files
Cleanup context
1 parent ce63e5d commit 25a6fca

File tree

1 file changed

+38
-114
lines changed

1 file changed

+38
-114
lines changed

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 38 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,22 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
434434
self.emit_err(UnknownMetaItem { span, item: found, expected: options })
435435
}
436436

437+
fn emit_parse_error(
438+
&self,
439+
span: Span,
440+
reason: AttributeParseErrorReason<'_>,
441+
) -> ErrorGuaranteed {
442+
self.emit_err(AttributeParseError {
443+
span,
444+
attr_span: self.attr_span,
445+
template: self.template.clone(),
446+
path: self.attr_path.clone(),
447+
description: self.parsed_description,
448+
reason,
449+
suggestions: self.suggestions(),
450+
})
451+
}
452+
437453
/// error that a string literal was expected.
438454
/// You can optionally give the literal you did find (which you found not to be a string literal)
439455
/// which can make better errors. For example, if the literal was a byte string it will suggest
@@ -443,133 +459,56 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
443459
span: Span,
444460
actual_literal: Option<&MetaItemLit>,
445461
) -> ErrorGuaranteed {
446-
self.emit_err(AttributeParseError {
462+
self.emit_parse_error(
447463
span,
448-
attr_span: self.attr_span,
449-
template: self.template.clone(),
450-
path: self.attr_path.clone(),
451-
description: self.parsed_description,
452-
reason: AttributeParseErrorReason::ExpectedStringLiteral {
464+
AttributeParseErrorReason::ExpectedStringLiteral {
453465
byte_string: actual_literal.and_then(|i| {
454466
i.kind.is_bytestr().then(|| self.sess().source_map().start_point(i.span))
455467
}),
456468
},
457-
suggestions: self.suggestions(),
458-
})
469+
)
459470
}
460471

461472
pub(crate) fn expected_integer_literal(&self, span: Span) -> ErrorGuaranteed {
462-
self.emit_err(AttributeParseError {
463-
span,
464-
attr_span: self.attr_span,
465-
template: self.template.clone(),
466-
path: self.attr_path.clone(),
467-
description: self.parsed_description,
468-
reason: AttributeParseErrorReason::ExpectedIntegerLiteral,
469-
suggestions: self.suggestions(),
470-
})
473+
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedIntegerLiteral)
471474
}
472475

473476
pub(crate) fn expected_list(&self, span: Span) -> ErrorGuaranteed {
474-
self.emit_err(AttributeParseError {
475-
span,
476-
attr_span: self.attr_span,
477-
template: self.template.clone(),
478-
path: self.attr_path.clone(),
479-
description: self.parsed_description,
480-
reason: AttributeParseErrorReason::ExpectedList,
481-
suggestions: self.suggestions(),
482-
})
477+
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedList)
483478
}
484479

485-
pub(crate) fn expected_no_args(&self, args_span: Span) -> ErrorGuaranteed {
486-
self.emit_err(AttributeParseError {
487-
span: args_span,
488-
attr_span: self.attr_span,
489-
template: self.template.clone(),
490-
path: self.attr_path.clone(),
491-
description: self.parsed_description,
492-
reason: AttributeParseErrorReason::ExpectedNoArgs,
493-
suggestions: self.suggestions(),
494-
})
480+
pub(crate) fn expected_no_args(&self, span: Span) -> ErrorGuaranteed {
481+
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedNoArgs)
495482
}
496483

497484
/// emit an error that a `name` was expected here
498485
pub(crate) fn expected_identifier(&self, span: Span) -> ErrorGuaranteed {
499-
self.emit_err(AttributeParseError {
500-
span,
501-
attr_span: self.attr_span,
502-
template: self.template.clone(),
503-
path: self.attr_path.clone(),
504-
description: self.parsed_description,
505-
reason: AttributeParseErrorReason::ExpectedIdentifier,
506-
suggestions: self.suggestions(),
507-
})
486+
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedIdentifier)
508487
}
509488

510489
/// emit an error that a `name = value` pair was expected at this span. The symbol can be given for
511490
/// a nicer error message talking about the specific name that was found lacking a value.
512491
pub(crate) fn expected_name_value(&self, span: Span, name: Option<Symbol>) -> ErrorGuaranteed {
513-
self.emit_err(AttributeParseError {
514-
span,
515-
attr_span: self.attr_span,
516-
template: self.template.clone(),
517-
path: self.attr_path.clone(),
518-
description: self.parsed_description,
519-
reason: AttributeParseErrorReason::ExpectedNameValue(name),
520-
suggestions: self.suggestions(),
521-
})
492+
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedNameValue(name))
522493
}
523494

524495
/// emit an error that a `name = value` pair was found where that name was already seen.
525496
pub(crate) fn duplicate_key(&self, span: Span, key: Symbol) -> ErrorGuaranteed {
526-
self.emit_err(AttributeParseError {
527-
span,
528-
attr_span: self.attr_span,
529-
template: self.template.clone(),
530-
path: self.attr_path.clone(),
531-
description: self.parsed_description,
532-
reason: AttributeParseErrorReason::DuplicateKey(key),
533-
suggestions: self.suggestions(),
534-
})
497+
self.emit_parse_error(span, AttributeParseErrorReason::DuplicateKey(key))
535498
}
536499

537500
/// an error that should be emitted when a [`MetaItemOrLitParser`](crate::parser::MetaItemOrLitParser)
538501
/// was expected *not* to be a literal, but instead a meta item.
539502
pub(crate) fn unexpected_literal(&self, span: Span) -> ErrorGuaranteed {
540-
self.emit_err(AttributeParseError {
541-
span,
542-
attr_span: self.attr_span,
543-
template: self.template.clone(),
544-
path: self.attr_path.clone(),
545-
description: self.parsed_description,
546-
reason: AttributeParseErrorReason::UnexpectedLiteral,
547-
suggestions: self.suggestions(),
548-
})
503+
self.emit_parse_error(span, AttributeParseErrorReason::UnexpectedLiteral)
549504
}
550505

551506
pub(crate) fn expected_single_argument(&self, span: Span) -> ErrorGuaranteed {
552-
self.emit_err(AttributeParseError {
553-
span,
554-
attr_span: self.attr_span,
555-
template: self.template.clone(),
556-
path: self.attr_path.clone(),
557-
description: self.parsed_description,
558-
reason: AttributeParseErrorReason::ExpectedSingleArgument,
559-
suggestions: self.suggestions(),
560-
})
507+
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedSingleArgument)
561508
}
562509

563510
pub(crate) fn expected_at_least_one_argument(&self, span: Span) -> ErrorGuaranteed {
564-
self.emit_err(AttributeParseError {
565-
span,
566-
attr_span: self.attr_span,
567-
template: self.template.clone(),
568-
path: self.attr_path.clone(),
569-
description: self.parsed_description,
570-
reason: AttributeParseErrorReason::ExpectedAtLeastOneArgument,
571-
suggestions: self.suggestions(),
572-
})
511+
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedAtLeastOneArgument)
573512
}
574513

575514
/// produces an error along the lines of `expected one of [foo, meow]`
@@ -578,19 +517,14 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
578517
span: Span,
579518
possibilities: &[Symbol],
580519
) -> ErrorGuaranteed {
581-
self.emit_err(AttributeParseError {
520+
self.emit_parse_error(
582521
span,
583-
attr_span: self.attr_span,
584-
template: self.template.clone(),
585-
path: self.attr_path.clone(),
586-
description: self.parsed_description,
587-
reason: AttributeParseErrorReason::ExpectedSpecificArgument {
522+
AttributeParseErrorReason::ExpectedSpecificArgument {
588523
possibilities,
589524
strings: false,
590525
list: false,
591526
},
592-
suggestions: self.suggestions(),
593-
})
527+
)
594528
}
595529

596530
/// produces an error along the lines of `expected one of [foo, meow] as an argument`.
@@ -600,19 +534,14 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
600534
span: Span,
601535
possibilities: &[Symbol],
602536
) -> ErrorGuaranteed {
603-
self.emit_err(AttributeParseError {
537+
self.emit_parse_error(
604538
span,
605-
attr_span: self.attr_span,
606-
template: self.template.clone(),
607-
path: self.attr_path.clone(),
608-
description: self.parsed_description,
609-
reason: AttributeParseErrorReason::ExpectedSpecificArgument {
539+
AttributeParseErrorReason::ExpectedSpecificArgument {
610540
possibilities,
611541
strings: false,
612542
list: true,
613543
},
614-
suggestions: self.suggestions(),
615-
})
544+
)
616545
}
617546

618547
/// produces an error along the lines of `expected one of ["foo", "meow"]`
@@ -621,19 +550,14 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
621550
span: Span,
622551
possibilities: &[Symbol],
623552
) -> ErrorGuaranteed {
624-
self.emit_err(AttributeParseError {
553+
self.emit_parse_error(
625554
span,
626-
attr_span: self.attr_span,
627-
template: self.template.clone(),
628-
path: self.attr_path.clone(),
629-
description: self.parsed_description,
630-
reason: AttributeParseErrorReason::ExpectedSpecificArgument {
555+
AttributeParseErrorReason::ExpectedSpecificArgument {
631556
possibilities,
632557
strings: true,
633558
list: false,
634559
},
635-
suggestions: self.suggestions(),
636-
})
560+
)
637561
}
638562

639563
pub(crate) fn warn_empty_attribute(&mut self, span: Span) {

0 commit comments

Comments
 (0)