Skip to content

Commit 43f3afd

Browse files
Rework how the disallowed qualifier lints are generated
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
1 parent 16d152c commit 43f3afd

File tree

6 files changed

+165
-265
lines changed

6 files changed

+165
-265
lines changed

compiler/rustc_parse/src/errors.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2940,9 +2940,8 @@ pub(crate) struct DynAfterMut {
29402940
#[diag(parse_fn_pointer_cannot_be_const)]
29412941
pub(crate) struct FnPointerCannotBeConst {
29422942
#[primary_span]
2943-
pub span: Span,
29442943
#[label]
2945-
pub qualifier: Span,
2944+
pub span: Span,
29462945
#[suggestion(code = "", applicability = "maybe-incorrect", style = "verbose")]
29472946
pub suggestion: Span,
29482947
}
@@ -2951,9 +2950,8 @@ pub(crate) struct FnPointerCannotBeConst {
29512950
#[diag(parse_fn_pointer_cannot_be_async)]
29522951
pub(crate) struct FnPointerCannotBeAsync {
29532952
#[primary_span]
2954-
pub span: Span,
29552953
#[label]
2956-
pub qualifier: Span,
2954+
pub span: Span,
29572955
#[suggestion(code = "", applicability = "maybe-incorrect", style = "verbose")]
29582956
pub suggestion: Span,
29592957
}

compiler/rustc_parse/src/parser/item.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use super::{
2323
AttrWrapper, ExpKeywordPair, ExpTokenPair, FollowedByType, ForceCollect, Parser, PathStyle,
2424
Recovered, Trailing, UsePreAttrPos,
2525
};
26-
use crate::errors::{self, MacroExpandsToAdtField};
26+
use crate::errors::{self, FnPointerCannotBeAsync, FnPointerCannotBeConst, MacroExpandsToAdtField};
2727
use crate::{exp, fluent_generated as fluent};
2828

2929
impl<'a> Parser<'a> {
@@ -2402,7 +2402,7 @@ impl<'a> Parser<'a> {
24022402
case: Case,
24032403
) -> PResult<'a, (Ident, FnSig, Generics, Option<P<FnContract>>, Option<P<Block>>)> {
24042404
let fn_span = self.token.span;
2405-
let header = self.parse_fn_front_matter(vis, case)?; // `const ... fn`
2405+
let header = self.parse_fn_front_matter(vis, case, false)?; // `const ... fn`
24062406
let ident = self.parse_ident()?; // `foo`
24072407
let mut generics = self.parse_generics()?; // `<'a, T, ...>`
24082408
let decl = match self.parse_fn_decl(
@@ -2658,16 +2658,35 @@ impl<'a> Parser<'a> {
26582658
///
26592659
/// `vis` represents the visibility that was already parsed, if any. Use
26602660
/// `Visibility::Inherited` when no visibility is known.
2661+
///
2662+
/// If `is_fn_pointer_type`, we error on `const` and `async` qualifiers,
2663+
/// which are not allowed in function pointer types.
26612664
pub(super) fn parse_fn_front_matter(
26622665
&mut self,
26632666
orig_vis: &Visibility,
26642667
case: Case,
2668+
is_fn_pointer_type: bool,
26652669
) -> PResult<'a, FnHeader> {
26662670
let sp_start = self.token.span;
26672671
let constness = self.parse_constness(case);
2672+
if is_fn_pointer_type && let Const::Yes(const_span) = constness {
2673+
self.dcx().emit_err(FnPointerCannotBeConst {
2674+
span: const_span,
2675+
suggestion: const_span.until(self.token.span),
2676+
});
2677+
}
26682678

26692679
let async_start_sp = self.token.span;
26702680
let coroutine_kind = self.parse_coroutine_kind(case);
2681+
if is_fn_pointer_type
2682+
&& let Some(ast::CoroutineKind::Async { span: async_span, .. }) = coroutine_kind
2683+
{
2684+
self.dcx().emit_err(FnPointerCannotBeAsync {
2685+
span: async_span,
2686+
suggestion: async_span.until(self.token.span),
2687+
});
2688+
}
2689+
// FIXME(gen_blocks): emit a similar error for `gen fn()`
26712690

26722691
let unsafe_start_sp = self.token.span;
26732692
let safety = self.parse_safety(case);
@@ -2703,6 +2722,11 @@ impl<'a> Parser<'a> {
27032722
enum WrongKw {
27042723
Duplicated(Span),
27052724
Misplaced(Span),
2725+
/// `MisplacedDisallowedQualifier` is only used instead of `Misplaced`,
2726+
/// when the misplaced keyword is disallowed by `is_fn_pointer_type`.
2727+
/// In this case, we avoid generating the suggestion to swap around the keywords,
2728+
/// as we already generated a suggestion to remove the keyword earlier.
2729+
MisplacedDisallowedQualifier,
27062730
}
27072731

27082732
// We may be able to recover
@@ -2716,7 +2740,18 @@ impl<'a> Parser<'a> {
27162740
Const::Yes(sp) => Some(WrongKw::Duplicated(sp)),
27172741
Const::No => {
27182742
recover_constness = Const::Yes(self.token.span);
2719-
Some(WrongKw::Misplaced(async_start_sp))
2743+
if is_fn_pointer_type {
2744+
self.dcx().emit_err(FnPointerCannotBeConst {
2745+
span: self.token.span,
2746+
suggestion: self
2747+
.token
2748+
.span
2749+
.with_lo(self.prev_token.span.hi()),
2750+
});
2751+
Some(WrongKw::MisplacedDisallowedQualifier)
2752+
} else {
2753+
Some(WrongKw::Misplaced(async_start_sp))
2754+
}
27202755
}
27212756
}
27222757
} else if self.check_keyword(exp!(Async)) {
@@ -2742,7 +2777,18 @@ impl<'a> Parser<'a> {
27422777
closure_id: DUMMY_NODE_ID,
27432778
return_impl_trait_id: DUMMY_NODE_ID,
27442779
});
2745-
Some(WrongKw::Misplaced(unsafe_start_sp))
2780+
if is_fn_pointer_type {
2781+
self.dcx().emit_err(FnPointerCannotBeAsync {
2782+
span: self.token.span,
2783+
suggestion: self
2784+
.token
2785+
.span
2786+
.with_lo(self.prev_token.span.hi()),
2787+
});
2788+
Some(WrongKw::MisplacedDisallowedQualifier)
2789+
} else {
2790+
Some(WrongKw::Misplaced(unsafe_start_sp))
2791+
}
27462792
}
27472793
}
27482794
} else if self.check_keyword(exp!(Unsafe)) {

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use thin_vec::{ThinVec, thin_vec};
1515
use super::{Parser, PathStyle, SeqSep, TokenType, Trailing};
1616
use crate::errors::{
1717
self, DynAfterMut, ExpectedFnPathFoundFnKeyword, ExpectedMutOrConstInRawPointerType,
18-
FnPointerCannotBeAsync, FnPointerCannotBeConst, FnPtrWithGenerics, FnPtrWithGenericsSugg,
19-
HelpUseLatestEdition, InvalidDynKeyword, LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime,
20-
NestedCVariadicType, ReturnTypesUseThinArrow,
18+
FnPtrWithGenerics, FnPtrWithGenericsSugg, HelpUseLatestEdition, InvalidDynKeyword,
19+
LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime, NestedCVariadicType,
20+
ReturnTypesUseThinArrow,
2121
};
2222
use crate::{exp, maybe_recover_from_interpolated_ty_qpath};
2323

@@ -669,62 +669,13 @@ impl<'a> Parser<'a> {
669669
tokens: None,
670670
};
671671
let span_start = self.token.span;
672-
let ast::FnHeader { ext, safety, constness, coroutine_kind } =
673-
self.parse_fn_front_matter(&inherited_vis, Case::Sensitive)?;
674-
let fn_start_lo = self.prev_token.span.lo();
672+
let ast::FnHeader { ext, safety, .. } =
673+
self.parse_fn_front_matter(&inherited_vis, Case::Sensitive, true)?;
675674
if self.may_recover() && self.token == TokenKind::Lt {
676675
self.recover_fn_ptr_with_generics(lo, &mut params, param_insertion_point)?;
677676
}
678677
let decl = self.parse_fn_decl(|_| false, AllowPlus::No, recover_return_sign)?;
679-
let whole_span = lo.to(self.prev_token.span);
680-
681-
// Order/parsing of "front matter" follows:
682-
// `<constness> <coroutine_kind> <safety> <extern> fn()`
683-
// ^ ^ ^ ^ ^
684-
// | | | | fn_start_lo
685-
// | | | ext_sp.lo
686-
// | | safety_sp.lo
687-
// | coroutine_sp.lo
688-
// const_sp.lo
689-
if let ast::Const::Yes(const_span) = constness {
690-
let next_token_lo = if let Some(
691-
ast::CoroutineKind::Async { span, .. }
692-
| ast::CoroutineKind::Gen { span, .. }
693-
| ast::CoroutineKind::AsyncGen { span, .. },
694-
) = coroutine_kind
695-
{
696-
span.lo()
697-
} else if let ast::Safety::Unsafe(span) | ast::Safety::Safe(span) = safety {
698-
span.lo()
699-
} else if let ast::Extern::Implicit(span) | ast::Extern::Explicit(_, span) = ext {
700-
span.lo()
701-
} else {
702-
fn_start_lo
703-
};
704-
let sugg_span = const_span.with_hi(next_token_lo);
705-
self.dcx().emit_err(FnPointerCannotBeConst {
706-
span: whole_span,
707-
qualifier: const_span,
708-
suggestion: sugg_span,
709-
});
710-
}
711-
if let Some(ast::CoroutineKind::Async { span: async_span, .. }) = coroutine_kind {
712-
let next_token_lo = if let ast::Safety::Unsafe(span) | ast::Safety::Safe(span) = safety
713-
{
714-
span.lo()
715-
} else if let ast::Extern::Implicit(span) | ast::Extern::Explicit(_, span) = ext {
716-
span.lo()
717-
} else {
718-
fn_start_lo
719-
};
720-
let sugg_span = async_span.with_hi(next_token_lo);
721-
self.dcx().emit_err(FnPointerCannotBeAsync {
722-
span: whole_span,
723-
qualifier: async_span,
724-
suggestion: sugg_span,
725-
});
726-
}
727-
// FIXME(gen_blocks): emit a similar error for `gen fn()`
678+
728679
let decl_span = span_start.to(self.prev_token.span);
729680
Ok(TyKind::BareFn(P(BareFnTy { ext, safety, generic_params: params, decl, decl_span })))
730681
}

tests/ui/parser/bad-fn-ptr-qualifier.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,15 @@ pub type FTT6 = for<'a> unsafe extern "C" fn();
2323
//~^ ERROR an `fn` pointer type cannot be `const`
2424
//~| ERROR an `fn` pointer type cannot be `async`
2525

26+
// Tests with qualifiers in the wrong order
27+
pub type W1 = unsafe fn();
28+
//~^ ERROR an `fn` pointer type cannot be `const`
29+
//~| ERROR expected one of `extern` or `fn`, found keyword `const`
30+
pub type W2 = unsafe fn();
31+
//~^ ERROR an `fn` pointer type cannot be `async`
32+
//~| ERROR expected one of `extern` or `fn`, found keyword `async`
33+
pub type W3 = for<'a> unsafe fn();
34+
//~^ ERROR an `fn` pointer type cannot be `const`
35+
//~| ERROR expected one of `extern` or `fn`, found keyword `const`
36+
2637
fn main() {}

0 commit comments

Comments
 (0)