1use std::fmt::Write;
2use std::mem;
3
4use ast::token::IdentIsRaw;
5use rustc_ast as ast;
6use rustc_ast::ast::*;
7use rustc_ast::token::{self, Delimiter, InvisibleOrigin, MetaVarKind, TokenKind};
8use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
9use rustc_ast::util::case::Case;
10use rustc_ast_pretty::pprust;
11use rustc_errors::codes::*;
12use rustc_errors::{Applicability, PResult, StashKey, msg, struct_span_code_err};
13use rustc_session::lint::builtin::VARARGS_WITHOUT_PATTERN;
14use rustc_span::edit_distance::edit_distance;
15use rustc_span::edition::Edition;
16use rustc_span::{DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, respan, sym};
17use thin_vec::{ThinVec, thin_vec};
18use tracing::debug;
19
20use super::diagnostics::{ConsumeClosingDelim, dummy_arg};
21use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
22use super::{
23 AllowConstBlockItems, AttrWrapper, ExpKeywordPair, ExpTokenPair, FollowedByType, ForceCollect,
24 Parser, PathStyle, Recovered, Trailing, UsePreAttrPos,
25};
26use crate::errors::{self, FnPointerCannotBeAsync, FnPointerCannotBeConst, MacroExpandsToAdtField};
27use crate::exp;
28
29impl<'a> Parser<'a> {
30 pub fn parse_crate_mod(&mut self) -> PResult<'a, ast::Crate> {
32 let (attrs, items, spans) = self.parse_mod(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eof,
token_type: crate::parser::token_type::TokenType::Eof,
}exp!(Eof))?;
33 Ok(ast::Crate { attrs, items, spans, id: DUMMY_NODE_ID, is_placeholder: false })
34 }
35
36 fn parse_item_mod(&mut self, attrs: &mut AttrVec) -> PResult<'a, ItemKind> {
38 let safety = self.parse_safety(Case::Sensitive);
39 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Mod,
token_type: crate::parser::token_type::TokenType::KwMod,
}exp!(Mod))?;
40 let ident = self.parse_ident()?;
41 let mod_kind = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
42 ModKind::Unloaded
43 } else {
44 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace))?;
45 let (inner_attrs, items, inner_span) = self.parse_mod(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace))?;
46 attrs.extend(inner_attrs);
47 ModKind::Loaded(items, Inline::Yes, inner_span)
48 };
49 Ok(ItemKind::Mod(safety, ident, mod_kind))
50 }
51
52 pub fn parse_mod(
57 &mut self,
58 term: ExpTokenPair,
59 ) -> PResult<'a, (AttrVec, ThinVec<Box<Item>>, ModSpans)> {
60 let lo = self.token.span;
61 let attrs = self.parse_inner_attributes()?;
62
63 let post_attr_lo = self.token.span;
64 let mut items: ThinVec<Box<_>> = ThinVec::new();
65
66 loop {
69 while self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) {} let Some(item) = self.parse_item(ForceCollect::No, AllowConstBlockItems::Yes)? else {
71 break;
72 };
73 items.push(item);
74 }
75
76 if !self.eat(term) {
77 let token_str = super::token_descr(&self.token);
78 if !self.maybe_consume_incorrect_semicolon(items.last().map(|x| &**x)) {
79 let is_let = self.token.is_keyword(kw::Let);
80 let is_let_mut = is_let && self.look_ahead(1, |t| t.is_keyword(kw::Mut));
81 let let_has_ident = is_let && !is_let_mut && self.is_kw_followed_by_ident(kw::Let);
82
83 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected item, found {0}",
token_str))
})format!("expected item, found {token_str}");
84 let mut err = self.dcx().struct_span_err(self.token.span, msg);
85
86 let label = if is_let {
87 "`let` cannot be used for global variables"
88 } else {
89 "expected item"
90 };
91 err.span_label(self.token.span, label);
92
93 if is_let {
94 if is_let_mut {
95 err.help("consider using `static` and a `Mutex` instead of `let mut`");
96 } else if let_has_ident {
97 err.span_suggestion_short(
98 self.token.span,
99 "consider using `static` or `const` instead of `let`",
100 "static",
101 Applicability::MaybeIncorrect,
102 );
103 } else {
104 err.help("consider using `static` or `const` instead of `let`");
105 }
106 }
107 err.note("for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>");
108 return Err(err);
109 }
110 }
111
112 let inject_use_span = post_attr_lo.data().with_hi(post_attr_lo.lo());
113 let mod_spans = ModSpans { inner_span: lo.to(self.prev_token.span), inject_use_span };
114 Ok((attrs, items, mod_spans))
115 }
116}
117
118enum ReuseKind {
119 Path,
120 Impl,
121}
122
123impl<'a> Parser<'a> {
124 pub fn parse_item(
125 &mut self,
126 force_collect: ForceCollect,
127 allow_const_block_items: AllowConstBlockItems,
128 ) -> PResult<'a, Option<Box<Item>>> {
129 let fn_parse_mode =
130 FnParseMode { req_name: |_, _| true, context: FnContext::Free, req_body: true };
131 self.parse_item_(fn_parse_mode, force_collect, allow_const_block_items)
132 .map(|i| i.map(Box::new))
133 }
134
135 fn parse_item_(
136 &mut self,
137 fn_parse_mode: FnParseMode,
138 force_collect: ForceCollect,
139 const_block_items_allowed: AllowConstBlockItems,
140 ) -> PResult<'a, Option<Item>> {
141 self.recover_vcs_conflict_marker();
142 let attrs = self.parse_outer_attributes()?;
143 self.recover_vcs_conflict_marker();
144 self.parse_item_common(
145 attrs,
146 true,
147 false,
148 fn_parse_mode,
149 force_collect,
150 const_block_items_allowed,
151 )
152 }
153
154 pub(super) fn parse_item_common(
155 &mut self,
156 attrs: AttrWrapper,
157 mac_allowed: bool,
158 attrs_allowed: bool,
159 fn_parse_mode: FnParseMode,
160 force_collect: ForceCollect,
161 allow_const_block_items: AllowConstBlockItems,
162 ) -> PResult<'a, Option<Item>> {
163 if let Some(item) = self.eat_metavar_seq(MetaVarKind::Item, |this| {
164 this.parse_item(ForceCollect::Yes, allow_const_block_items)
165 }) {
166 let mut item = item.expect("an actual item");
167 attrs.prepend_to_nt_inner(&mut item.attrs);
168 return Ok(Some(*item));
169 }
170
171 self.collect_tokens(None, attrs, force_collect, |this, mut attrs| {
172 let lo = this.token.span;
173 let vis = this.parse_visibility(FollowedByType::No)?;
174 let mut def = this.parse_defaultness();
175 let kind = this.parse_item_kind(
176 &mut attrs,
177 mac_allowed,
178 allow_const_block_items,
179 lo,
180 &vis,
181 &mut def,
182 fn_parse_mode,
183 Case::Sensitive,
184 )?;
185 if let Some(kind) = kind {
186 this.error_on_unconsumed_default(def, &kind);
187 let span = lo.to(this.prev_token.span);
188 let id = DUMMY_NODE_ID;
189 let item = Item { attrs, id, kind, vis, span, tokens: None };
190 return Ok((Some(item), Trailing::No, UsePreAttrPos::No));
191 }
192
193 if !#[allow(non_exhaustive_omitted_patterns)] match vis.kind {
VisibilityKind::Inherited => true,
_ => false,
}matches!(vis.kind, VisibilityKind::Inherited) {
195 let mut err = this
196 .dcx()
197 .create_err(errors::VisibilityNotFollowedByItem { span: vis.span, vis });
198 if let Some((ident, _)) = this.token.ident()
199 && !ident.is_used_keyword()
200 && let Some((similar_kw, is_incorrect_case)) = ident
201 .name
202 .find_similar(&rustc_span::symbol::used_keywords(|| ident.span.edition()))
203 {
204 err.subdiagnostic(errors::MisspelledKw {
205 similar_kw: similar_kw.to_string(),
206 span: ident.span,
207 is_incorrect_case,
208 });
209 }
210 err.emit();
211 }
212
213 if let Defaultness::Default(span) = def {
214 this.dcx().emit_err(errors::DefaultNotFollowedByItem { span });
215 } else if let Defaultness::Final(span) = def {
216 this.dcx().emit_err(errors::FinalNotFollowedByItem { span });
217 }
218
219 if !attrs_allowed {
220 this.recover_attrs_no_item(&attrs)?;
221 }
222 Ok((None, Trailing::No, UsePreAttrPos::No))
223 })
224 }
225
226 fn error_on_unconsumed_default(&self, def: Defaultness, kind: &ItemKind) {
228 match def {
229 Defaultness::Default(span) => {
230 self.dcx().emit_err(errors::InappropriateDefault {
231 span,
232 article: kind.article(),
233 descr: kind.descr(),
234 });
235 }
236 Defaultness::Final(span) => {
237 self.dcx().emit_err(errors::InappropriateFinal {
238 span,
239 article: kind.article(),
240 descr: kind.descr(),
241 });
242 }
243 Defaultness::Implicit => (),
244 }
245 }
246
247 fn parse_item_kind(
249 &mut self,
250 attrs: &mut AttrVec,
251 macros_allowed: bool,
252 allow_const_block_items: AllowConstBlockItems,
253 lo: Span,
254 vis: &Visibility,
255 def: &mut Defaultness,
256 fn_parse_mode: FnParseMode,
257 case: Case,
258 ) -> PResult<'a, Option<ItemKind>> {
259 let check_pub = def == &Defaultness::Implicit;
260 let mut def_ = || mem::replace(def, Defaultness::Implicit);
261
262 let info = if !self.is_use_closure() && self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Use,
token_type: crate::parser::token_type::TokenType::KwUse,
}exp!(Use), case) {
263 self.parse_use_item()?
264 } else if self.check_fn_front_matter(check_pub, case) {
265 let defaultness = def_();
267 if let Defaultness::Default(span) = defaultness {
268 self.psess.gated_spans.gate(sym::min_specialization, span);
272 self.psess.gated_spans.ungate_last(sym::specialization, span);
273 }
274 let (ident, sig, generics, contract, body) =
275 self.parse_fn(attrs, fn_parse_mode, lo, vis, case)?;
276 ItemKind::Fn(Box::new(Fn {
277 defaultness,
278 ident,
279 sig,
280 generics,
281 contract,
282 body,
283 define_opaque: None,
284 eii_impls: ThinVec::new(),
285 }))
286 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern), case) {
287 if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Crate,
token_type: crate::parser::token_type::TokenType::KwCrate,
}exp!(Crate), case) {
288 self.parse_item_extern_crate()?
290 } else {
291 self.parse_item_foreign_mod(attrs, Safety::Default)?
293 }
294 } else if self.is_unsafe_foreign_mod() {
295 let safety = self.parse_safety(Case::Sensitive);
297 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern))?;
298 self.parse_item_foreign_mod(attrs, safety)?
299 } else if let Some(safety) = self.parse_global_static_front_matter(case) {
300 let mutability = self.parse_mutability();
302 self.parse_static_item(safety, mutability)?
303 } else if self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Trait,
token_type: crate::parser::token_type::TokenType::KwTrait,
}exp!(Trait), case) || self.check_trait_front_matter() {
304 self.parse_item_trait(attrs, lo)?
306 } else if self.check_impl_frontmatter(0) {
307 self.parse_item_impl(attrs, def_(), false)?
309 } else if let AllowConstBlockItems::Yes | AllowConstBlockItems::DoesNotMatter =
310 allow_const_block_items
311 && self.check_inline_const(0)
312 {
313 if let AllowConstBlockItems::DoesNotMatter = allow_const_block_items {
315 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/item.rs:315",
"rustc_parse::parser::item", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/item.rs"),
::tracing_core::__macro_support::Option::Some(315u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::parser::item"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Parsing a const block item that does not matter: {0:?}",
self.token.span) as &dyn Value))])
});
} else { ; }
};debug!("Parsing a const block item that does not matter: {:?}", self.token.span);
316 };
317 ItemKind::ConstBlock(self.parse_const_block_item()?)
318 } else if let Const::Yes(const_span) = self.parse_constness(case) {
319 self.recover_const_mut(const_span);
321 self.recover_missing_kw_before_item()?;
322 let (ident, generics, ty, rhs_kind) = self.parse_const_item(false, const_span)?;
323 ItemKind::Const(Box::new(ConstItem {
324 defaultness: def_(),
325 ident,
326 generics,
327 ty,
328 rhs_kind,
329 define_opaque: None,
330 }))
331 } else if let Some(kind) = self.is_reuse_item() {
332 self.parse_item_delegation(attrs, def_(), kind)?
333 } else if self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Mod,
token_type: crate::parser::token_type::TokenType::KwMod,
}exp!(Mod), case)
334 || self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe), case) && self.is_keyword_ahead(1, &[kw::Mod])
335 {
336 self.parse_item_mod(attrs)?
338 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Type,
token_type: crate::parser::token_type::TokenType::KwType,
}exp!(Type), case) {
339 if let Const::Yes(const_span) = self.parse_constness(case) {
340 self.recover_const_mut(const_span);
342 self.recover_missing_kw_before_item()?;
343 let (ident, generics, ty, rhs_kind) = self.parse_const_item(true, const_span)?;
344 self.psess.gated_spans.gate(sym::mgca_type_const_syntax, lo.to(const_span));
347 ItemKind::Const(Box::new(ConstItem {
348 defaultness: def_(),
349 ident,
350 generics,
351 ty,
352 rhs_kind,
353 define_opaque: None,
354 }))
355 } else {
356 self.parse_type_alias(def_())?
358 }
359 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Enum,
token_type: crate::parser::token_type::TokenType::KwEnum,
}exp!(Enum), case) {
360 self.parse_item_enum()?
362 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Struct,
token_type: crate::parser::token_type::TokenType::KwStruct,
}exp!(Struct), case) {
363 self.parse_item_struct()?
365 } else if self.is_kw_followed_by_ident(kw::Union) {
366 self.bump(); self.parse_item_union()?
369 } else if self.is_builtin() {
370 return self.parse_item_builtin();
372 } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Macro,
token_type: crate::parser::token_type::TokenType::KwMacro,
}exp!(Macro), case) {
373 self.parse_item_decl_macro(lo)?
375 } else if let IsMacroRulesItem::Yes { has_bang } = self.is_macro_rules_item() {
376 self.parse_item_macro_rules(vis, has_bang)?
378 } else if self.isnt_macro_invocation()
379 && (self.token.is_ident_named(sym::import)
380 || self.token.is_ident_named(sym::using)
381 || self.token.is_ident_named(sym::include)
382 || self.token.is_ident_named(sym::require))
383 {
384 return self.recover_import_as_use();
385 } else if self.isnt_macro_invocation() && vis.kind.is_pub() {
386 self.recover_missing_kw_before_item()?;
387 return Ok(None);
388 } else if self.isnt_macro_invocation() && case == Case::Sensitive {
389 _ = def_;
390
391 return self.parse_item_kind(
393 attrs,
394 macros_allowed,
395 allow_const_block_items,
396 lo,
397 vis,
398 def,
399 fn_parse_mode,
400 Case::Insensitive,
401 );
402 } else if macros_allowed && self.check_path() {
403 if self.isnt_macro_invocation() {
404 self.recover_missing_kw_before_item()?;
405 }
406 ItemKind::MacCall(Box::new(self.parse_item_macro(vis)?))
408 } else {
409 return Ok(None);
410 };
411 Ok(Some(info))
412 }
413
414 fn recover_import_as_use(&mut self) -> PResult<'a, Option<ItemKind>> {
415 let span = self.token.span;
416 let token_name = super::token_descr(&self.token);
417 let snapshot = self.create_snapshot_for_diagnostic();
418 self.bump();
419 match self.parse_use_item() {
420 Ok(u) => {
421 self.dcx().emit_err(errors::RecoverImportAsUse { span, token_name });
422 Ok(Some(u))
423 }
424 Err(e) => {
425 e.cancel();
426 self.restore_snapshot(snapshot);
427 Ok(None)
428 }
429 }
430 }
431
432 fn parse_use_item(&mut self) -> PResult<'a, ItemKind> {
433 let tree = self.parse_use_tree()?;
434 if let Err(mut e) = self.expect_semi() {
435 match tree.kind {
436 UseTreeKind::Glob(_) => {
437 e.note("the wildcard token must be last on the path");
438 }
439 UseTreeKind::Nested { .. } => {
440 e.note("glob-like brace syntax must be last on the path");
441 }
442 _ => (),
443 }
444 return Err(e);
445 }
446 Ok(ItemKind::Use(tree))
447 }
448
449 pub(super) fn is_path_start_item(&mut self) -> bool {
451 self.is_kw_followed_by_ident(kw::Union) || self.is_reuse_item().is_some() || self.check_trait_front_matter() || self.is_async_fn() || #[allow(non_exhaustive_omitted_patterns)] match self.is_macro_rules_item() {
IsMacroRulesItem::Yes { .. } => true,
_ => false,
}matches!(self.is_macro_rules_item(), IsMacroRulesItem::Yes{..}) }
457
458 fn is_reuse_item(&mut self) -> Option<ReuseKind> {
459 if !self.token.is_keyword(kw::Reuse) {
460 return None;
461 }
462
463 if self.look_ahead(1, |t| t.is_path_start() && *t != token::PathSep) {
465 Some(ReuseKind::Path)
466 } else if self.check_impl_frontmatter(1) {
467 Some(ReuseKind::Impl)
468 } else {
469 None
470 }
471 }
472
473 fn isnt_macro_invocation(&mut self) -> bool {
475 self.check_ident() && self.look_ahead(1, |t| *t != token::Bang && *t != token::PathSep)
476 }
477
478 fn recover_missing_kw_before_item(&mut self) -> PResult<'a, ()> {
481 let is_pub = self.prev_token.is_keyword(kw::Pub);
482 let is_const = self.prev_token.is_keyword(kw::Const);
483 let ident_span = self.token.span;
484 let span = if is_pub { self.prev_token.span.to(ident_span) } else { ident_span };
485 let insert_span = ident_span.shrink_to_lo();
486
487 let ident = if self.token.is_ident()
488 && (!is_const || self.look_ahead(1, |t| *t == token::OpenParen))
489 && self.look_ahead(1, |t| {
490 #[allow(non_exhaustive_omitted_patterns)] match t.kind {
token::Lt | token::OpenBrace | token::OpenParen => true,
_ => false,
}matches!(t.kind, token::Lt | token::OpenBrace | token::OpenParen)
491 }) {
492 self.parse_ident_common(true).unwrap()
493 } else {
494 return Ok(());
495 };
496
497 let mut found_generics = false;
498 if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Lt,
token_type: crate::parser::token_type::TokenType::Lt,
}exp!(Lt)) {
499 found_generics = true;
500 self.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Gt,
token_type: crate::parser::token_type::TokenType::Gt,
}exp!(Gt)]);
501 self.bump(); }
503
504 let err = if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
505 if self.look_ahead(1, |t| *t == token::CloseBrace) {
507 Some(errors::MissingKeywordForItemDefinition::EnumOrStruct { span })
509 } else if self.look_ahead(2, |t| *t == token::Colon)
510 || self.look_ahead(3, |t| *t == token::Colon)
511 {
512 Some(errors::MissingKeywordForItemDefinition::Struct { span, insert_span, ident })
514 } else {
515 Some(errors::MissingKeywordForItemDefinition::Enum { span, insert_span, ident })
516 }
517 } else if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
518 self.bump(); let is_method = self.recover_self_param();
521
522 self.consume_block(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen), ConsumeClosingDelim::Yes);
523
524 let err = if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::RArrow,
token_type: crate::parser::token_type::TokenType::RArrow,
}exp!(RArrow)) || self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
525 self.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)]);
526 self.bump(); self.consume_block(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), ConsumeClosingDelim::Yes);
528 if is_method {
529 errors::MissingKeywordForItemDefinition::Method { span, insert_span, ident }
530 } else {
531 errors::MissingKeywordForItemDefinition::Function { span, insert_span, ident }
532 }
533 } else if is_pub && self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
534 errors::MissingKeywordForItemDefinition::Struct { span, insert_span, ident }
535 } else {
536 errors::MissingKeywordForItemDefinition::Ambiguous {
537 span,
538 subdiag: if found_generics {
539 None
540 } else if let Ok(snippet) = self.span_to_snippet(ident_span) {
541 Some(errors::AmbiguousMissingKwForItemSub::SuggestMacro {
542 span: ident_span,
543 snippet,
544 })
545 } else {
546 Some(errors::AmbiguousMissingKwForItemSub::HelpMacro)
547 },
548 }
549 };
550 Some(err)
551 } else if found_generics {
552 Some(errors::MissingKeywordForItemDefinition::Ambiguous { span, subdiag: None })
553 } else {
554 None
555 };
556
557 if let Some(err) = err { Err(self.dcx().create_err(err)) } else { Ok(()) }
558 }
559
560 fn parse_item_builtin(&mut self) -> PResult<'a, Option<ItemKind>> {
561 Ok(None)
563 }
564
565 fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, MacCall> {
567 let path = self.parse_path(PathStyle::Mod)?; self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang))?; match self.parse_delim_args() {
570 Ok(args) => {
572 self.eat_semi_for_macro_if_needed(&args, Some(&path));
573 self.complain_if_pub_macro(vis, false);
574 Ok(MacCall { path, args })
575 }
576
577 Err(mut err) => {
578 if self.token.is_ident()
580 && let [segment] = path.segments.as_slice()
581 && edit_distance("macro_rules", &segment.ident.to_string(), 2).is_some()
582 {
583 err.span_suggestion(
584 path.span,
585 "perhaps you meant to define a macro",
586 "macro_rules",
587 Applicability::MachineApplicable,
588 );
589 }
590 Err(err)
591 }
592 }
593 }
594
595 fn recover_attrs_no_item(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
597 let ([start @ end] | [start, .., end]) = attrs else {
598 return Ok(());
599 };
600 let msg = if end.is_doc_comment() {
601 "expected item after doc comment"
602 } else {
603 "expected item after attributes"
604 };
605 let mut err = self.dcx().struct_span_err(end.span, msg);
606 if end.is_doc_comment() {
607 err.span_label(end.span, "this doc comment doesn't document anything");
608 } else if self.token == TokenKind::Semi {
609 err.span_suggestion_verbose(
610 self.token.span,
611 "consider removing this semicolon",
612 "",
613 Applicability::MaybeIncorrect,
614 );
615 }
616 if let [.., penultimate, _] = attrs {
617 err.span_label(start.span.to(penultimate.span), "other attributes here");
618 }
619 Err(err)
620 }
621
622 fn is_async_fn(&self) -> bool {
623 self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
624 }
625
626 fn parse_polarity(&mut self) -> ast::ImplPolarity {
627 if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) && self.look_ahead(1, |t| t.can_begin_type()) {
629 self.psess.gated_spans.gate(sym::negative_impls, self.token.span);
630 self.bump(); ast::ImplPolarity::Negative(self.prev_token.span)
632 } else {
633 ast::ImplPolarity::Positive
634 }
635 }
636
637 fn parse_item_impl(
652 &mut self,
653 attrs: &mut AttrVec,
654 defaultness: Defaultness,
655 is_reuse: bool,
656 ) -> PResult<'a, ItemKind> {
657 let mut constness = self.parse_constness(Case::Sensitive);
658 let safety = self.parse_safety(Case::Sensitive);
659 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Impl,
token_type: crate::parser::token_type::TokenType::KwImpl,
}exp!(Impl))?;
660
661 let mut generics = if self.choose_generics_over_qpath(0) {
663 self.parse_generics()?
664 } else {
665 let mut generics = Generics::default();
666 generics.span = self.prev_token.span.shrink_to_hi();
669 generics
670 };
671
672 if let Const::No = constness {
673 constness = self.parse_constness(Case::Sensitive);
675 }
676
677 if let Const::Yes(span) = constness {
678 self.psess.gated_spans.gate(sym::const_trait_impl, span);
679 }
680
681 if (self.token_uninterpolated_span().at_least_rust_2018()
683 && self.token.is_keyword(kw::Async))
684 || self.is_kw_followed_by_ident(kw::Async)
685 {
686 self.bump();
687 self.dcx().emit_err(errors::AsyncImpl { span: self.prev_token.span });
688 }
689
690 let polarity = self.parse_polarity();
691
692 let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
694 {
695 let span = self.prev_token.span.between(self.token.span);
696 return Err(self.dcx().create_err(errors::MissingTraitInTraitImpl {
697 span,
698 for_span: span.to(self.token.span),
699 }));
700 } else {
701 self.parse_ty_with_generics_recovery(&generics)?
702 };
703
704 let has_for = self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::For,
token_type: crate::parser::token_type::TokenType::KwFor,
}exp!(For));
706 let missing_for_span = self.prev_token.span.between(self.token.span);
707
708 let ty_second = if self.token == token::DotDot {
709 self.bump(); Some(self.mk_ty(self.prev_token.span, TyKind::Dummy))
716 } else if has_for || self.token.can_begin_type() {
717 Some(self.parse_ty()?)
718 } else {
719 None
720 };
721
722 generics.where_clause = self.parse_where_clause()?;
723
724 let impl_items = if is_reuse {
725 Default::default()
726 } else {
727 self.parse_item_list(attrs, |p| p.parse_impl_item(ForceCollect::No))?
728 };
729
730 let (of_trait, self_ty) = match ty_second {
731 Some(ty_second) => {
732 if !has_for {
734 self.dcx().emit_err(errors::MissingForInTraitImpl { span: missing_for_span });
735 }
736
737 let ty_first = *ty_first;
738 let path = match ty_first.kind {
739 TyKind::Path(None, path) => path,
741 other => {
742 if let TyKind::ImplTrait(_, bounds) = other
743 && let [bound] = bounds.as_slice()
744 && let GenericBound::Trait(poly_trait_ref) = bound
745 {
746 let extra_impl_kw = ty_first.span.until(bound.span());
750 self.dcx().emit_err(errors::ExtraImplKeywordInTraitImpl {
751 extra_impl_kw,
752 impl_trait_span: ty_first.span,
753 });
754 poly_trait_ref.trait_ref.path.clone()
755 } else {
756 return Err(self.dcx().create_err(
757 errors::ExpectedTraitInTraitImplFoundType { span: ty_first.span },
758 ));
759 }
760 }
761 };
762 let trait_ref = TraitRef { path, ref_id: ty_first.id };
763
764 let of_trait =
765 Some(Box::new(TraitImplHeader { defaultness, safety, polarity, trait_ref }));
766 (of_trait, ty_second)
767 }
768 None => {
769 let self_ty = ty_first;
770 let error = |modifier, modifier_name, modifier_span| {
771 self.dcx().create_err(errors::TraitImplModifierInInherentImpl {
772 span: self_ty.span,
773 modifier,
774 modifier_name,
775 modifier_span,
776 self_ty: self_ty.span,
777 })
778 };
779
780 if let Safety::Unsafe(span) = safety {
781 error("unsafe", "unsafe", span).with_code(E0197).emit();
782 }
783 if let ImplPolarity::Negative(span) = polarity {
784 error("!", "negative", span).emit();
785 }
786 if let Defaultness::Default(def_span) = defaultness {
787 error("default", "default", def_span).emit();
788 }
789 if let Const::Yes(span) = constness {
790 self.psess.gated_spans.gate(sym::const_trait_impl, span);
791 }
792 (None, self_ty)
793 }
794 };
795
796 Ok(ItemKind::Impl(Impl { generics, of_trait, self_ty, items: impl_items, constness }))
797 }
798
799 fn parse_item_delegation(
800 &mut self,
801 attrs: &mut AttrVec,
802 defaultness: Defaultness,
803 kind: ReuseKind,
804 ) -> PResult<'a, ItemKind> {
805 let span = self.token.span;
806 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Reuse,
token_type: crate::parser::token_type::TokenType::KwReuse,
}exp!(Reuse))?;
807
808 let item_kind = match kind {
809 ReuseKind::Path => self.parse_path_like_delegation(),
810 ReuseKind::Impl => self.parse_impl_delegation(span, attrs, defaultness),
811 }?;
812
813 self.psess.gated_spans.gate(sym::fn_delegation, span.to(self.prev_token.span));
814
815 Ok(item_kind)
816 }
817
818 fn parse_delegation_body(&mut self) -> PResult<'a, Option<Box<Block>>> {
819 Ok(if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
820 Some(self.parse_block()?)
821 } else {
822 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi))?;
823 None
824 })
825 }
826
827 fn parse_impl_delegation(
828 &mut self,
829 span: Span,
830 attrs: &mut AttrVec,
831 defaultness: Defaultness,
832 ) -> PResult<'a, ItemKind> {
833 let mut impl_item = self.parse_item_impl(attrs, defaultness, true)?;
834 let ItemKind::Impl(Impl { items, of_trait, .. }) = &mut impl_item else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
835
836 let until_expr_span = span.to(self.prev_token.span);
837
838 let Some(of_trait) = of_trait else {
839 return Err(self
840 .dcx()
841 .create_err(errors::ImplReuseInherentImpl { span: until_expr_span }));
842 };
843
844 let body = self.parse_delegation_body()?;
845 let whole_reuse_span = span.to(self.prev_token.span);
846
847 items.push(Box::new(AssocItem {
848 id: DUMMY_NODE_ID,
849 attrs: Default::default(),
850 span: whole_reuse_span,
851 tokens: None,
852 vis: Visibility {
853 kind: VisibilityKind::Inherited,
854 span: whole_reuse_span,
855 tokens: None,
856 },
857 kind: AssocItemKind::DelegationMac(Box::new(DelegationMac {
858 qself: None,
859 prefix: of_trait.trait_ref.path.clone(),
860 suffixes: None,
861 body,
862 })),
863 }));
864
865 Ok(impl_item)
866 }
867
868 fn parse_path_like_delegation(&mut self) -> PResult<'a, ItemKind> {
869 let (qself, path) = if self.eat_lt() {
870 let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
871 (Some(qself), path)
872 } else {
873 (None, self.parse_path(PathStyle::Expr)?)
874 };
875
876 let rename = |this: &mut Self| {
877 Ok(if this.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::As,
token_type: crate::parser::token_type::TokenType::KwAs,
}exp!(As)) { Some(this.parse_ident()?) } else { None })
878 };
879
880 Ok(if self.eat_path_sep() {
881 let suffixes = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Star,
token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star)) {
882 None
883 } else {
884 let parse_suffix = |p: &mut Self| Ok((p.parse_path_segment_ident()?, rename(p)?));
885 Some(self.parse_delim_comma_seq(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), parse_suffix)?.0)
886 };
887
888 ItemKind::DelegationMac(Box::new(DelegationMac {
889 qself,
890 prefix: path,
891 suffixes,
892 body: self.parse_delegation_body()?,
893 }))
894 } else {
895 let rename = rename(self)?;
896 let ident = rename.unwrap_or_else(|| path.segments.last().unwrap().ident);
897
898 ItemKind::Delegation(Box::new(Delegation {
899 id: DUMMY_NODE_ID,
900 qself,
901 path,
902 ident,
903 rename,
904 body: self.parse_delegation_body()?,
905 from_glob: false,
906 }))
907 })
908 }
909
910 fn parse_item_list<T>(
911 &mut self,
912 attrs: &mut AttrVec,
913 mut parse_item: impl FnMut(&mut Parser<'a>) -> PResult<'a, Option<Option<T>>>,
914 ) -> PResult<'a, ThinVec<T>> {
915 let open_brace_span = self.token.span;
916
917 if self.token == TokenKind::Semi {
919 self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span });
920 self.bump();
921 return Ok(ThinVec::new());
922 }
923
924 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace))?;
925 attrs.extend(self.parse_inner_attributes()?);
926
927 let mut items = ThinVec::new();
928 while !self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)) {
929 if self.recover_doc_comment_before_brace() {
930 continue;
931 }
932 self.recover_vcs_conflict_marker();
933 match parse_item(self) {
934 Ok(None) => {
935 let mut is_unnecessary_semicolon = !items.is_empty()
936 && self
954 .span_to_snippet(self.prev_token.span)
955 .is_ok_and(|snippet| snippet == "}")
956 && self.token == token::Semi;
957 let mut semicolon_span = self.token.span;
958 if !is_unnecessary_semicolon {
959 is_unnecessary_semicolon =
961 self.token == token::OpenBrace && self.prev_token == token::Semi;
962 semicolon_span = self.prev_token.span;
963 }
964 let non_item_span = self.token.span;
966 let is_let = self.token.is_keyword(kw::Let);
967
968 let mut err =
969 self.dcx().struct_span_err(non_item_span, "non-item in item list");
970 self.consume_block(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), ConsumeClosingDelim::Yes);
971 if is_let {
972 err.span_suggestion_verbose(
973 non_item_span,
974 "consider using `const` instead of `let` for associated const",
975 "const",
976 Applicability::MachineApplicable,
977 );
978 } else {
979 err.span_label(open_brace_span, "item list starts here")
980 .span_label(non_item_span, "non-item starts here")
981 .span_label(self.prev_token.span, "item list ends here");
982 }
983 if is_unnecessary_semicolon {
984 err.span_suggestion(
985 semicolon_span,
986 "consider removing this semicolon",
987 "",
988 Applicability::MaybeIncorrect,
989 );
990 }
991 err.emit();
992 break;
993 }
994 Ok(Some(item)) => items.extend(item),
995 Err(err) => {
996 self.consume_block(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), ConsumeClosingDelim::Yes);
997 err.with_span_label(
998 open_brace_span,
999 "while parsing this item list starting here",
1000 )
1001 .with_span_label(self.prev_token.span, "the item list ends here")
1002 .emit();
1003 break;
1004 }
1005 }
1006 }
1007 Ok(items)
1008 }
1009
1010 fn recover_doc_comment_before_brace(&mut self) -> bool {
1012 if let token::DocComment(..) = self.token.kind {
1013 if self.look_ahead(1, |tok| tok == &token::CloseBrace) {
1014 {
self.dcx().struct_span_err(self.token.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("found a documentation comment that doesn\'t document anything"))
})).with_code(E0584)
}struct_span_code_err!(
1016 self.dcx(),
1017 self.token.span,
1018 E0584,
1019 "found a documentation comment that doesn't document anything",
1020 )
1021 .with_span_label(self.token.span, "this doc comment doesn't document anything")
1022 .with_help(
1023 "doc comments must come before what they document, if a comment was \
1024 intended use `//`",
1025 )
1026 .emit();
1027 self.bump();
1028 return true;
1029 }
1030 }
1031 false
1032 }
1033
1034 fn parse_defaultness(&mut self) -> Defaultness {
1036 if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Default,
token_type: crate::parser::token_type::TokenType::KwDefault,
}exp!(Default))
1040 && self.look_ahead(1, |t| t.is_non_raw_ident_where(|i| i.name != kw::As))
1041 {
1042 self.psess.gated_spans.gate(sym::specialization, self.token.span);
1043 self.bump(); Defaultness::Default(self.prev_token_uninterpolated_span())
1045 } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Final,
token_type: crate::parser::token_type::TokenType::KwFinal,
}exp!(Final)) {
1046 self.psess.gated_spans.gate(sym::final_associated_functions, self.prev_token.span);
1047 Defaultness::Final(self.prev_token_uninterpolated_span())
1048 } else {
1049 Defaultness::Implicit
1050 }
1051 }
1052
1053 fn is_trait_with_maybe_impl_restriction_in_front(&self, dist: usize) -> bool {
1055 if self.is_keyword_ahead(dist, &[kw::Trait]) {
1057 return true;
1058 }
1059 if !self.is_keyword_ahead(dist, &[kw::Impl])
1061 || !self.look_ahead(dist + 1, |t| t == &token::OpenParen)
1062 {
1063 return false;
1064 }
1065 if self.is_keyword_ahead(dist + 2, &[kw::Crate, kw::Super, kw::SelfLower])
1067 && self.look_ahead(dist + 3, |t| t == &token::CloseParen)
1068 && self.is_keyword_ahead(dist + 4, &[kw::Trait])
1069 {
1070 return true;
1071 }
1072 self.tree_look_ahead(dist + 2, |t| {
1077 if let TokenTree::Token(token, _) = t { token.is_keyword(kw::Trait) } else { false }
1078 })
1079 .unwrap_or(false)
1080 }
1081
1082 fn check_trait_front_matter(&mut self) -> bool {
1084 if self.is_trait_with_maybe_impl_restriction_in_front(0) {
1086 return true;
1087 }
1088 if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Auto,
token_type: crate::parser::token_type::TokenType::KwAuto,
}exp!(Auto)) && self.is_trait_with_maybe_impl_restriction_in_front(1) {
1090 return true;
1091 }
1092 if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe))
1094 && (self.is_trait_with_maybe_impl_restriction_in_front(1)
1095 || self.is_keyword_ahead(1, &[kw::Auto])
1096 && self.is_trait_with_maybe_impl_restriction_in_front(2))
1097 {
1098 return true;
1099 }
1100 if !self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const)) {
1102 return false;
1103 }
1104 if self.is_trait_with_maybe_impl_restriction_in_front(1) {
1106 return true;
1107 }
1108 if self.is_keyword_ahead(1, &[kw::Unsafe, kw::Auto])
1110 && self.is_trait_with_maybe_impl_restriction_in_front(2)
1111 {
1112 return true;
1113 }
1114 self.is_keyword_ahead(1, &[kw::Unsafe])
1116 && self.is_keyword_ahead(2, &[kw::Auto])
1117 && self.is_trait_with_maybe_impl_restriction_in_front(3)
1118 }
1119
1120 fn parse_item_trait(&mut self, attrs: &mut AttrVec, lo: Span) -> PResult<'a, ItemKind> {
1127 let constness = self.parse_constness(Case::Sensitive);
1128 if let Const::Yes(span) = constness {
1129 self.psess.gated_spans.gate(sym::const_trait_impl, span);
1130 }
1131 let safety = self.parse_safety(Case::Sensitive);
1132 let is_auto = if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Auto,
token_type: crate::parser::token_type::TokenType::KwAuto,
}exp!(Auto)) {
1134 self.psess.gated_spans.gate(sym::auto_traits, self.prev_token.span);
1135 IsAuto::Yes
1136 } else {
1137 IsAuto::No
1138 };
1139
1140 let impl_restriction = self.parse_impl_restriction()?;
1141
1142 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Trait,
token_type: crate::parser::token_type::TokenType::KwTrait,
}exp!(Trait))?;
1143 let ident = self.parse_ident()?;
1144 let mut generics = self.parse_generics()?;
1145
1146 let had_colon = self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon));
1148 let span_at_colon = self.prev_token.span;
1149 let bounds = if had_colon { self.parse_generic_bounds()? } else { Vec::new() };
1150
1151 let span_before_eq = self.prev_token.span;
1152 if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) {
1153 if had_colon {
1155 let span = span_at_colon.to(span_before_eq);
1156 self.dcx().emit_err(errors::BoundsNotAllowedOnTraitAliases { span });
1157 }
1158
1159 let bounds = self.parse_generic_bounds()?;
1160 generics.where_clause = self.parse_where_clause()?;
1161 self.expect_semi()?;
1162
1163 let whole_span = lo.to(self.prev_token.span);
1164 if is_auto == IsAuto::Yes {
1165 self.dcx().emit_err(errors::TraitAliasCannotBeAuto { span: whole_span });
1166 }
1167 if let Safety::Unsafe(_) = safety {
1168 self.dcx().emit_err(errors::TraitAliasCannotBeUnsafe { span: whole_span });
1169 }
1170 if let RestrictionKind::Restricted { .. } = impl_restriction.kind {
1171 self.dcx().emit_err(errors::TraitAliasCannotBeImplRestricted { span: whole_span });
1172 }
1173
1174 self.psess.gated_spans.gate(sym::trait_alias, whole_span);
1175
1176 Ok(ItemKind::TraitAlias(Box::new(TraitAlias { constness, ident, generics, bounds })))
1177 } else {
1178 generics.where_clause = self.parse_where_clause()?;
1180 let items = self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?;
1181 Ok(ItemKind::Trait(Box::new(Trait {
1182 constness,
1183 is_auto,
1184 safety,
1185 impl_restriction,
1186 ident,
1187 generics,
1188 bounds,
1189 items,
1190 })))
1191 }
1192 }
1193
1194 pub fn parse_impl_item(
1195 &mut self,
1196 force_collect: ForceCollect,
1197 ) -> PResult<'a, Option<Option<Box<AssocItem>>>> {
1198 let fn_parse_mode =
1199 FnParseMode { req_name: |_, _| true, context: FnContext::Impl, req_body: true };
1200 self.parse_assoc_item(fn_parse_mode, force_collect)
1201 }
1202
1203 pub fn parse_trait_item(
1204 &mut self,
1205 force_collect: ForceCollect,
1206 ) -> PResult<'a, Option<Option<Box<AssocItem>>>> {
1207 let fn_parse_mode = FnParseMode {
1208 req_name: |edition, _| edition >= Edition::Edition2018,
1209 context: FnContext::Trait,
1210 req_body: false,
1211 };
1212 self.parse_assoc_item(fn_parse_mode, force_collect)
1213 }
1214
1215 fn parse_assoc_item(
1217 &mut self,
1218 fn_parse_mode: FnParseMode,
1219 force_collect: ForceCollect,
1220 ) -> PResult<'a, Option<Option<Box<AssocItem>>>> {
1221 Ok(self
1222 .parse_item_(
1223 fn_parse_mode,
1224 force_collect,
1225 AllowConstBlockItems::DoesNotMatter, )?
1227 .map(|Item { attrs, id, span, vis, kind, tokens }| {
1228 let kind = match AssocItemKind::try_from(kind) {
1229 Ok(kind) => kind,
1230 Err(kind) => match kind {
1231 ItemKind::Static(box StaticItem {
1232 ident,
1233 ty,
1234 safety: _,
1235 mutability: _,
1236 expr,
1237 define_opaque,
1238 }) => {
1239 self.dcx().emit_err(errors::AssociatedStaticItemNotAllowed { span });
1240 AssocItemKind::Const(Box::new(ConstItem {
1241 defaultness: Defaultness::Implicit,
1242 ident,
1243 generics: Generics::default(),
1244 ty,
1245 rhs_kind: ConstItemRhsKind::Body { rhs: expr },
1246 define_opaque,
1247 }))
1248 }
1249 _ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"),
1250 },
1251 };
1252 Some(Box::new(Item { attrs, id, span, vis, kind, tokens }))
1253 }))
1254 }
1255
1256 fn parse_type_alias(&mut self, defaultness: Defaultness) -> PResult<'a, ItemKind> {
1262 let ident = self.parse_ident()?;
1263 let mut generics = self.parse_generics()?;
1264
1265 let bounds = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) { self.parse_generic_bounds()? } else { Vec::new() };
1267 generics.where_clause = self.parse_where_clause()?;
1268
1269 let ty = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) { Some(self.parse_ty()?) } else { None };
1270
1271 let after_where_clause = self.parse_where_clause()?;
1272
1273 self.expect_semi()?;
1274
1275 Ok(ItemKind::TyAlias(Box::new(TyAlias {
1276 defaultness,
1277 ident,
1278 generics,
1279 after_where_clause,
1280 bounds,
1281 ty,
1282 })))
1283 }
1284
1285 fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
1295 let lo = self.token.span;
1296
1297 let mut prefix =
1298 ast::Path { segments: ThinVec::new(), span: lo.shrink_to_lo(), tokens: None };
1299 let kind =
1300 if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) || self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Star,
token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star)) || self.is_import_coupler() {
1301 let mod_sep_ctxt = self.token.span.ctxt();
1303 if self.eat_path_sep() {
1304 prefix
1305 .segments
1306 .push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
1307 }
1308
1309 self.parse_use_tree_glob_or_nested()?
1310 } else {
1311 prefix = self.parse_path(PathStyle::Mod)?;
1313
1314 if self.eat_path_sep() {
1315 self.parse_use_tree_glob_or_nested()?
1316 } else {
1317 while self.eat_noexpect(&token::Colon) {
1319 self.dcx()
1320 .emit_err(errors::SingleColonImportPath { span: self.prev_token.span });
1321
1322 self.parse_path_segments(&mut prefix.segments, PathStyle::Mod, None)?;
1324 prefix.span = lo.to(self.prev_token.span);
1325 }
1326
1327 UseTreeKind::Simple(self.parse_rename()?)
1328 }
1329 };
1330
1331 Ok(UseTree { prefix, kind })
1332 }
1333
1334 fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
1336 Ok(if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Star,
token_type: crate::parser::token_type::TokenType::Star,
}exp!(Star)) {
1337 UseTreeKind::Glob(self.prev_token.span)
1338 } else {
1339 let lo = self.token.span;
1340 UseTreeKind::Nested {
1341 items: self.parse_use_tree_list()?,
1342 span: lo.to(self.prev_token.span),
1343 }
1344 })
1345 }
1346
1347 fn parse_use_tree_list(&mut self) -> PResult<'a, ThinVec<(UseTree, ast::NodeId)>> {
1353 self.parse_delim_comma_seq(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), |p| {
1354 p.recover_vcs_conflict_marker();
1355 Ok((p.parse_use_tree()?, DUMMY_NODE_ID))
1356 })
1357 .map(|(r, _)| r)
1358 }
1359
1360 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
1361 if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::As,
token_type: crate::parser::token_type::TokenType::KwAs,
}exp!(As)) {
1362 self.parse_ident_or_underscore().map(Some)
1363 } else {
1364 Ok(None)
1365 }
1366 }
1367
1368 fn parse_ident_or_underscore(&mut self) -> PResult<'a, Ident> {
1369 match self.token.ident() {
1370 Some((ident @ Ident { name: kw::Underscore, .. }, IdentIsRaw::No)) => {
1371 self.bump();
1372 Ok(ident)
1373 }
1374 _ => self.parse_ident(),
1375 }
1376 }
1377
1378 fn parse_item_extern_crate(&mut self) -> PResult<'a, ItemKind> {
1387 let orig_ident = self.parse_crate_name_with_dashes()?;
1389 let (orig_name, item_ident) = if let Some(rename) = self.parse_rename()? {
1390 (Some(orig_ident.name), rename)
1391 } else {
1392 (None, orig_ident)
1393 };
1394 self.expect_semi()?;
1395 Ok(ItemKind::ExternCrate(orig_name, item_ident))
1396 }
1397
1398 fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, Ident> {
1399 let ident = if self.token.is_keyword(kw::SelfLower) {
1400 self.parse_path_segment_ident()
1401 } else {
1402 self.parse_ident()
1403 }?;
1404
1405 let dash = crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Minus,
token_type: crate::parser::token_type::TokenType::Minus,
}exp!(Minus);
1406 if self.token != dash.tok {
1407 return Ok(ident);
1408 }
1409
1410 let mut dashes = ::alloc::vec::Vec::new()vec![];
1412 let mut idents = ::alloc::vec::Vec::new()vec![];
1413 while self.eat(dash) {
1414 dashes.push(self.prev_token.span);
1415 idents.push(self.parse_ident()?);
1416 }
1417
1418 let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
1419 let mut fixed_name = ident.name.to_string();
1420 for part in idents {
1421 fixed_name.write_fmt(format_args!("_{0}", part.name))write!(fixed_name, "_{}", part.name).unwrap();
1422 }
1423
1424 self.dcx().emit_err(errors::ExternCrateNameWithDashes {
1425 span: fixed_name_sp,
1426 sugg: errors::ExternCrateNameWithDashesSugg { dashes },
1427 });
1428
1429 Ok(Ident::from_str_and_span(&fixed_name, fixed_name_sp))
1430 }
1431
1432 fn parse_item_foreign_mod(
1443 &mut self,
1444 attrs: &mut AttrVec,
1445 mut safety: Safety,
1446 ) -> PResult<'a, ItemKind> {
1447 let extern_span = self.prev_token_uninterpolated_span();
1448 let abi = self.parse_abi(); if safety == Safety::Default
1451 && self.token.is_keyword(kw::Unsafe)
1452 && self.look_ahead(1, |t| *t == token::OpenBrace)
1453 {
1454 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)).unwrap_err().emit();
1455 safety = Safety::Unsafe(self.token.span);
1456 let _ = self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe));
1457 }
1458 Ok(ItemKind::ForeignMod(ast::ForeignMod {
1459 extern_span,
1460 safety,
1461 abi,
1462 items: self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No))?,
1463 }))
1464 }
1465
1466 pub fn parse_foreign_item(
1468 &mut self,
1469 force_collect: ForceCollect,
1470 ) -> PResult<'a, Option<Option<Box<ForeignItem>>>> {
1471 let fn_parse_mode = FnParseMode {
1472 req_name: |_, is_dot_dot_dot| is_dot_dot_dot == IsDotDotDot::No,
1473 context: FnContext::Free,
1474 req_body: false,
1475 };
1476 Ok(self
1477 .parse_item_(
1478 fn_parse_mode,
1479 force_collect,
1480 AllowConstBlockItems::DoesNotMatter, )?
1482 .map(|Item { attrs, id, span, vis, kind, tokens }| {
1483 let kind = match ForeignItemKind::try_from(kind) {
1484 Ok(kind) => kind,
1485 Err(kind) => match kind {
1486 ItemKind::Const(box ConstItem { ident, ty, rhs_kind, .. }) => {
1487 let const_span = Some(span.with_hi(ident.span.lo()))
1488 .filter(|span| span.can_be_used_for_suggestions());
1489 self.dcx().emit_err(errors::ExternItemCannotBeConst {
1490 ident_span: ident.span,
1491 const_span,
1492 });
1493 ForeignItemKind::Static(Box::new(StaticItem {
1494 ident,
1495 ty,
1496 mutability: Mutability::Not,
1497 expr: match rhs_kind {
1498 ConstItemRhsKind::Body { rhs } => rhs,
1499 ConstItemRhsKind::TypeConst { rhs: Some(anon) } => {
1500 Some(anon.value)
1501 }
1502 ConstItemRhsKind::TypeConst { rhs: None } => None,
1503 },
1504 safety: Safety::Default,
1505 define_opaque: None,
1506 }))
1507 }
1508 _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
1509 },
1510 };
1511 Some(Box::new(Item { attrs, id, span, vis, kind, tokens }))
1512 }))
1513 }
1514
1515 fn error_bad_item_kind<T>(&self, span: Span, kind: &ItemKind, ctx: &'static str) -> Option<T> {
1516 let span = self.psess.source_map().guess_head_span(span);
1518 let descr = kind.descr();
1519 let help = match kind {
1520 ItemKind::DelegationMac(deleg) if deleg.suffixes.is_none() => false,
1521 _ => true,
1522 };
1523 self.dcx().emit_err(errors::BadItemKind { span, descr, ctx, help });
1524 None
1525 }
1526
1527 fn is_use_closure(&self) -> bool {
1528 if self.token.is_keyword(kw::Use) {
1529 self.look_ahead(1, |token| {
1531 let dist =
1533 if token.is_keyword(kw::Move) || token.is_keyword(kw::Async) { 2 } else { 1 };
1534
1535 self.look_ahead(dist, |token| #[allow(non_exhaustive_omitted_patterns)] match token.kind {
token::Or | token::OrOr => true,
_ => false,
}matches!(token.kind, token::Or | token::OrOr))
1536 })
1537 } else {
1538 false
1539 }
1540 }
1541
1542 fn is_unsafe_foreign_mod(&self) -> bool {
1543 if !self.token.is_keyword(kw::Unsafe) {
1545 return false;
1546 }
1547 if !self.is_keyword_ahead(1, &[kw::Extern]) {
1549 return false;
1550 }
1551
1552 let n = if self.look_ahead(2, |t| t.can_begin_string_literal()) { 3 } else { 2 };
1554
1555 self.tree_look_ahead(n, |t| #[allow(non_exhaustive_omitted_patterns)] match t {
TokenTree::Delimited(_, _, Delimiter::Brace, _) => true,
_ => false,
}matches!(t, TokenTree::Delimited(_, _, Delimiter::Brace, _)))
1560 == Some(true)
1561 }
1562
1563 fn parse_global_static_front_matter(&mut self, case: Case) -> Option<Safety> {
1564 let is_global_static = if self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Static,
token_type: crate::parser::token_type::TokenType::KwStatic,
}exp!(Static), case) {
1565 !self.look_ahead(1, |token| {
1567 if token.is_keyword_case(kw::Move, case) || token.is_keyword_case(kw::Use, case) {
1568 return true;
1569 }
1570 #[allow(non_exhaustive_omitted_patterns)] match token.kind {
token::Or | token::OrOr => true,
_ => false,
}matches!(token.kind, token::Or | token::OrOr)
1571 })
1572 } else {
1573 (self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe), case)
1575 || self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Safe,
token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe), case))
1576 && self.look_ahead(1, |t| t.is_keyword_case(kw::Static, case))
1577 };
1578
1579 if is_global_static {
1580 let safety = self.parse_safety(case);
1581 let _ = self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Static,
token_type: crate::parser::token_type::TokenType::KwStatic,
}exp!(Static), case);
1582 Some(safety)
1583 } else {
1584 None
1585 }
1586 }
1587
1588 fn recover_const_mut(&mut self, const_span: Span) {
1590 if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Mut,
token_type: crate::parser::token_type::TokenType::KwMut,
}exp!(Mut)) {
1591 let span = self.prev_token.span;
1592 self.dcx()
1593 .emit_err(errors::ConstGlobalCannotBeMutable { ident_span: span, const_span });
1594 } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Let,
token_type: crate::parser::token_type::TokenType::KwLet,
}exp!(Let)) {
1595 let span = self.prev_token.span;
1596 self.dcx().emit_err(errors::ConstLetMutuallyExclusive { span: const_span.to(span) });
1597 }
1598 }
1599
1600 fn parse_const_block_item(&mut self) -> PResult<'a, ConstBlockItem> {
1601 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const))?;
1602 let const_span = self.prev_token.span;
1603 self.psess.gated_spans.gate(sym::const_block_items, const_span);
1604 let block = self.parse_block()?;
1605 Ok(ConstBlockItem { id: DUMMY_NODE_ID, span: const_span.to(block.span), block })
1606 }
1607
1608 fn parse_static_item(
1615 &mut self,
1616 safety: Safety,
1617 mutability: Mutability,
1618 ) -> PResult<'a, ItemKind> {
1619 let ident = self.parse_ident()?;
1620
1621 if self.token == TokenKind::Lt && self.may_recover() {
1622 let generics = self.parse_generics()?;
1623 self.dcx().emit_err(errors::StaticWithGenerics { span: generics.span });
1624 }
1625
1626 let ty = match (self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)), self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) | self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi))) {
1629 (true, false) => self.parse_ty()?,
1630 (colon, _) => self.recover_missing_global_item_type(colon, Some(mutability)),
1633 };
1634
1635 let expr = if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) { Some(self.parse_expr()?) } else { None };
1636
1637 self.expect_semi()?;
1638
1639 let item = StaticItem { ident, ty, safety, mutability, expr, define_opaque: None };
1640 Ok(ItemKind::Static(Box::new(item)))
1641 }
1642
1643 fn parse_const_item(
1652 &mut self,
1653 const_arg: bool,
1654 const_span: Span,
1655 ) -> PResult<'a, (Ident, Generics, Box<Ty>, ConstItemRhsKind)> {
1656 let ident = self.parse_ident_or_underscore()?;
1657
1658 let mut generics = self.parse_generics()?;
1659
1660 if !generics.span.is_empty() {
1663 self.psess.gated_spans.gate(sym::generic_const_items, generics.span);
1664 }
1665
1666 let ty = match (
1669 self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)),
1670 self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) | self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) | self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Where,
token_type: crate::parser::token_type::TokenType::KwWhere,
}exp!(Where)),
1671 ) {
1672 (true, false) => self.parse_ty()?,
1673 (colon, _) => self.recover_missing_global_item_type(colon, None),
1675 };
1676
1677 let before_where_clause =
1680 if self.may_recover() { self.parse_where_clause()? } else { WhereClause::default() };
1681
1682 let rhs = match (self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)), const_arg) {
1683 (true, true) => ConstItemRhsKind::TypeConst {
1684 rhs: Some(self.parse_expr_anon_const(|_, _| MgcaDisambiguation::Direct)?),
1685 },
1686 (true, false) => ConstItemRhsKind::Body { rhs: Some(self.parse_expr()?) },
1687 (false, true) => ConstItemRhsKind::TypeConst { rhs: None },
1688 (false, false) => ConstItemRhsKind::Body { rhs: None },
1689 };
1690
1691 let after_where_clause = self.parse_where_clause()?;
1692
1693 if before_where_clause.has_where_token
1697 && let Some(rhs_span) = rhs.span()
1698 {
1699 self.dcx().emit_err(errors::WhereClauseBeforeConstBody {
1700 span: before_where_clause.span,
1701 name: ident.span,
1702 body: rhs_span,
1703 sugg: if !after_where_clause.has_where_token {
1704 self.psess.source_map().span_to_snippet(rhs_span).ok().map(|body_s| {
1705 errors::WhereClauseBeforeConstBodySugg {
1706 left: before_where_clause.span.shrink_to_lo(),
1707 snippet: body_s,
1708 right: before_where_clause.span.shrink_to_hi().to(rhs_span),
1709 }
1710 })
1711 } else {
1712 None
1715 },
1716 });
1717 }
1718
1719 let mut predicates = before_where_clause.predicates;
1726 predicates.extend(after_where_clause.predicates);
1727 let where_clause = WhereClause {
1728 has_where_token: before_where_clause.has_where_token
1729 || after_where_clause.has_where_token,
1730 predicates,
1731 span: if after_where_clause.has_where_token {
1732 after_where_clause.span
1733 } else {
1734 before_where_clause.span
1735 },
1736 };
1737
1738 if where_clause.has_where_token {
1739 self.psess.gated_spans.gate(sym::generic_const_items, where_clause.span);
1740 }
1741
1742 generics.where_clause = where_clause;
1743
1744 if let Some(rhs) = self.try_recover_const_missing_semi(&rhs, const_span) {
1745 return Ok((ident, generics, ty, ConstItemRhsKind::Body { rhs: Some(rhs) }));
1746 }
1747 self.expect_semi()?;
1748
1749 Ok((ident, generics, ty, rhs))
1750 }
1751
1752 fn recover_missing_global_item_type(
1755 &mut self,
1756 colon_present: bool,
1757 m: Option<Mutability>,
1758 ) -> Box<Ty> {
1759 let kind = match m {
1762 Some(Mutability::Mut) => "static mut",
1763 Some(Mutability::Not) => "static",
1764 None => "const",
1765 };
1766
1767 let colon = match colon_present {
1768 true => "",
1769 false => ":",
1770 };
1771
1772 let span = self.prev_token.span.shrink_to_hi();
1773 let err = self.dcx().create_err(errors::MissingConstType { span, colon, kind });
1774 err.stash(span, StashKey::ItemNoType);
1775
1776 Box::new(Ty { kind: TyKind::Infer, span, id: ast::DUMMY_NODE_ID, tokens: None })
1779 }
1780
1781 fn parse_item_enum(&mut self) -> PResult<'a, ItemKind> {
1783 if self.token.is_keyword(kw::Struct) {
1784 let span = self.prev_token.span.to(self.token.span);
1785 let err = errors::EnumStructMutuallyExclusive { span };
1786 if self.look_ahead(1, |t| t.is_ident()) {
1787 self.bump();
1788 self.dcx().emit_err(err);
1789 } else {
1790 return Err(self.dcx().create_err(err));
1791 }
1792 }
1793
1794 let prev_span = self.prev_token.span;
1795 let ident = self.parse_ident()?;
1796 let mut generics = self.parse_generics()?;
1797 generics.where_clause = self.parse_where_clause()?;
1798
1799 let (variants, _) = if self.token == TokenKind::Semi {
1801 self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span });
1802 self.bump();
1803 (::thin_vec::ThinVec::new()thin_vec![], Trailing::No)
1804 } else {
1805 self.parse_delim_comma_seq(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace), |p| {
1806 p.parse_enum_variant(ident.span)
1807 })
1808 .map_err(|mut err| {
1809 err.span_label(ident.span, "while parsing this enum");
1810 if self.prev_token.is_non_reserved_ident() && self.token == token::Colon {
1812 let snapshot = self.create_snapshot_for_diagnostic();
1813 self.bump();
1814 match self.parse_ty() {
1815 Ok(_) => {
1816 err.span_suggestion_verbose(
1817 prev_span,
1818 "perhaps you meant to use `struct` here",
1819 "struct",
1820 Applicability::MaybeIncorrect,
1821 );
1822 }
1823 Err(e) => {
1824 e.cancel();
1825 }
1826 }
1827 self.restore_snapshot(snapshot);
1828 }
1829 self.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)]);
1830 self.bump(); err
1832 })?
1833 };
1834
1835 let enum_definition = EnumDef { variants: variants.into_iter().flatten().collect() };
1836 Ok(ItemKind::Enum(ident, generics, enum_definition))
1837 }
1838
1839 fn parse_enum_variant(&mut self, span: Span) -> PResult<'a, Option<Variant>> {
1840 self.recover_vcs_conflict_marker();
1841 let variant_attrs = self.parse_outer_attributes()?;
1842 self.recover_vcs_conflict_marker();
1843 let help = "enum variants can be `Variant`, `Variant = <integer>`, \
1844 `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`";
1845 self.collect_tokens(None, variant_attrs, ForceCollect::No, |this, variant_attrs| {
1846 let vlo = this.token.span;
1847
1848 let vis = this.parse_visibility(FollowedByType::No)?;
1849 if !this.recover_nested_adt_item(kw::Enum)? {
1850 return Ok((None, Trailing::No, UsePreAttrPos::No));
1851 }
1852 let ident = this.parse_field_ident("enum", vlo)?;
1853
1854 if this.token == token::Bang {
1855 if let Err(err) = this.unexpected() {
1856 err.with_note(rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("macros cannot expand to enum variants"))msg!("macros cannot expand to enum variants")).emit();
1857 }
1858
1859 this.bump();
1860 this.parse_delim_args()?;
1861
1862 return Ok((None, Trailing::from(this.token == token::Comma), UsePreAttrPos::No));
1863 }
1864
1865 let struct_def = if this.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
1866 let (fields, recovered) =
1868 match this.parse_record_struct_body("struct", ident.span, false) {
1869 Ok((fields, recovered)) => (fields, recovered),
1870 Err(mut err) => {
1871 if this.token == token::Colon {
1872 return Err(err);
1874 }
1875 this.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)]);
1876 this.bump(); err.span_label(span, "while parsing this enum");
1878 err.help(help);
1879 let guar = err.emit();
1880 (::thin_vec::ThinVec::new()thin_vec![], Recovered::Yes(guar))
1881 }
1882 };
1883 VariantData::Struct { fields, recovered }
1884 } else if this.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
1885 let body = match this.parse_tuple_struct_body() {
1886 Ok(body) => body,
1887 Err(mut err) => {
1888 if this.token == token::Colon {
1889 return Err(err);
1891 }
1892 this.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen)]);
1893 this.bump(); err.span_label(span, "while parsing this enum");
1895 err.help(help);
1896 err.emit();
1897 ::thin_vec::ThinVec::new()thin_vec![]
1898 }
1899 };
1900 VariantData::Tuple(body, DUMMY_NODE_ID)
1901 } else {
1902 VariantData::Unit(DUMMY_NODE_ID)
1903 };
1904
1905 let disr_expr = if this.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Eq,
token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) {
1906 Some(this.parse_expr_anon_const(|_, _| MgcaDisambiguation::AnonConst)?)
1907 } else {
1908 None
1909 };
1910
1911 let vr = ast::Variant {
1912 ident,
1913 vis,
1914 id: DUMMY_NODE_ID,
1915 attrs: variant_attrs,
1916 data: struct_def,
1917 disr_expr,
1918 span: vlo.to(this.prev_token.span),
1919 is_placeholder: false,
1920 };
1921
1922 Ok((Some(vr), Trailing::from(this.token == token::Comma), UsePreAttrPos::No))
1923 })
1924 .map_err(|mut err| {
1925 err.help(help);
1926 err
1927 })
1928 }
1929
1930 fn parse_item_struct(&mut self) -> PResult<'a, ItemKind> {
1932 let ident = self.parse_ident()?;
1933
1934 let mut generics = self.parse_generics()?;
1935
1936 let vdata = if self.token.is_keyword(kw::Where) {
1951 let tuple_struct_body;
1952 (generics.where_clause, tuple_struct_body) =
1953 self.parse_struct_where_clause(ident, generics.span)?;
1954
1955 if let Some(body) = tuple_struct_body {
1956 let body = VariantData::Tuple(body, DUMMY_NODE_ID);
1958 self.expect_semi()?;
1959 body
1960 } else if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
1961 VariantData::Unit(DUMMY_NODE_ID)
1963 } else {
1964 let (fields, recovered) = self.parse_record_struct_body(
1966 "struct",
1967 ident.span,
1968 generics.where_clause.has_where_token,
1969 )?;
1970 VariantData::Struct { fields, recovered }
1971 }
1972 } else if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
1974 VariantData::Unit(DUMMY_NODE_ID)
1975 } else if self.token == token::OpenBrace {
1977 let (fields, recovered) = self.parse_record_struct_body(
1978 "struct",
1979 ident.span,
1980 generics.where_clause.has_where_token,
1981 )?;
1982 VariantData::Struct { fields, recovered }
1983 } else if self.token == token::OpenParen {
1985 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1986 generics.where_clause = self.parse_where_clause()?;
1987 self.expect_semi()?;
1988 body
1989 } else {
1990 let err = errors::UnexpectedTokenAfterStructName::new(self.token.span, self.token);
1991 return Err(self.dcx().create_err(err));
1992 };
1993
1994 Ok(ItemKind::Struct(ident, generics, vdata))
1995 }
1996
1997 fn parse_item_union(&mut self) -> PResult<'a, ItemKind> {
1999 let ident = self.parse_ident()?;
2000
2001 let mut generics = self.parse_generics()?;
2002
2003 let vdata = if self.token.is_keyword(kw::Where) {
2004 generics.where_clause = self.parse_where_clause()?;
2005 let (fields, recovered) = self.parse_record_struct_body(
2006 "union",
2007 ident.span,
2008 generics.where_clause.has_where_token,
2009 )?;
2010 VariantData::Struct { fields, recovered }
2011 } else if self.token == token::OpenBrace {
2012 let (fields, recovered) = self.parse_record_struct_body(
2013 "union",
2014 ident.span,
2015 generics.where_clause.has_where_token,
2016 )?;
2017 VariantData::Struct { fields, recovered }
2018 } else {
2019 let token_str = super::token_descr(&self.token);
2020 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `where` or `{{` after union name, found {0}",
token_str))
})format!("expected `where` or `{{` after union name, found {token_str}");
2021 let mut err = self.dcx().struct_span_err(self.token.span, msg);
2022 err.span_label(self.token.span, "expected `where` or `{` after union name");
2023 return Err(err);
2024 };
2025
2026 Ok(ItemKind::Union(ident, generics, vdata))
2027 }
2028
2029 pub(crate) fn parse_record_struct_body(
2034 &mut self,
2035 adt_ty: &str,
2036 ident_span: Span,
2037 parsed_where: bool,
2038 ) -> PResult<'a, (ThinVec<FieldDef>, Recovered)> {
2039 let mut fields = ThinVec::new();
2040 let mut recovered = Recovered::No;
2041 if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
2042 while self.token != token::CloseBrace {
2043 match self.parse_field_def(adt_ty, ident_span) {
2044 Ok(field) => {
2045 fields.push(field);
2046 }
2047 Err(mut err) => {
2048 self.consume_block(
2049 crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace),
2050 crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace),
2051 ConsumeClosingDelim::No,
2052 );
2053 err.span_label(ident_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("while parsing this {0}", adt_ty))
})format!("while parsing this {adt_ty}"));
2054 let guar = err.emit();
2055 recovered = Recovered::Yes(guar);
2056 break;
2057 }
2058 }
2059 }
2060 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace))?;
2061 } else {
2062 let token_str = super::token_descr(&self.token);
2063 let where_str = if parsed_where { "" } else { "`where`, or " };
2064 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0}`{{` after struct name, found {1}",
where_str, token_str))
})format!("expected {where_str}`{{` after struct name, found {token_str}");
2065 let mut err = self.dcx().struct_span_err(self.token.span, msg);
2066 err.span_label(self.token.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0}`{{` after struct name",
where_str))
})format!("expected {where_str}`{{` after struct name",));
2067 return Err(err);
2068 }
2069
2070 Ok((fields, recovered))
2071 }
2072
2073 fn parse_unsafe_field(&mut self) -> Safety {
2074 if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe)) {
2076 let span = self.prev_token.span;
2077 self.psess.gated_spans.gate(sym::unsafe_fields, span);
2078 Safety::Unsafe(span)
2079 } else {
2080 Safety::Default
2081 }
2082 }
2083
2084 pub(super) fn parse_tuple_struct_body(&mut self) -> PResult<'a, ThinVec<FieldDef>> {
2085 self.parse_paren_comma_seq(|p| {
2088 let attrs = p.parse_outer_attributes()?;
2089 p.collect_tokens(None, attrs, ForceCollect::No, |p, attrs| {
2090 let mut snapshot = None;
2091 if p.is_vcs_conflict_marker(&TokenKind::Shl, &TokenKind::Lt) {
2092 snapshot = Some(p.create_snapshot_for_diagnostic());
2096 }
2097 let lo = p.token.span;
2098 let vis = match p.parse_visibility(FollowedByType::Yes) {
2099 Ok(vis) => vis,
2100 Err(err) => {
2101 if let Some(ref mut snapshot) = snapshot {
2102 snapshot.recover_vcs_conflict_marker();
2103 }
2104 return Err(err);
2105 }
2106 };
2107 let ty = match p.parse_ty() {
2110 Ok(ty) => ty,
2111 Err(err) => {
2112 if let Some(ref mut snapshot) = snapshot {
2113 snapshot.recover_vcs_conflict_marker();
2114 }
2115 return Err(err);
2116 }
2117 };
2118 let mut default = None;
2119 if p.token == token::Eq {
2120 let mut snapshot = p.create_snapshot_for_diagnostic();
2121 snapshot.bump();
2122 match snapshot.parse_expr_anon_const(|_, _| MgcaDisambiguation::AnonConst) {
2123 Ok(const_expr) => {
2124 let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
2125 p.psess.gated_spans.gate(sym::default_field_values, sp);
2126 p.restore_snapshot(snapshot);
2127 default = Some(const_expr);
2128 }
2129 Err(err) => {
2130 err.cancel();
2131 }
2132 }
2133 }
2134
2135 Ok((
2136 FieldDef {
2137 span: lo.to(ty.span),
2138 vis,
2139 safety: Safety::Default,
2140 ident: None,
2141 id: DUMMY_NODE_ID,
2142 ty,
2143 default,
2144 attrs,
2145 is_placeholder: false,
2146 },
2147 Trailing::from(p.token == token::Comma),
2148 UsePreAttrPos::No,
2149 ))
2150 })
2151 })
2152 .map(|(r, _)| r)
2153 }
2154
2155 fn parse_field_def(&mut self, adt_ty: &str, ident_span: Span) -> PResult<'a, FieldDef> {
2157 self.recover_vcs_conflict_marker();
2158 let attrs = self.parse_outer_attributes()?;
2159 self.recover_vcs_conflict_marker();
2160 self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
2161 let lo = this.token.span;
2162 let vis = this.parse_visibility(FollowedByType::No)?;
2163 let safety = this.parse_unsafe_field();
2164 this.parse_single_struct_field(adt_ty, lo, vis, safety, attrs, ident_span)
2165 .map(|field| (field, Trailing::No, UsePreAttrPos::No))
2166 })
2167 }
2168
2169 fn parse_single_struct_field(
2171 &mut self,
2172 adt_ty: &str,
2173 lo: Span,
2174 vis: Visibility,
2175 safety: Safety,
2176 attrs: AttrVec,
2177 ident_span: Span,
2178 ) -> PResult<'a, FieldDef> {
2179 let a_var = self.parse_name_and_ty(adt_ty, lo, vis, safety, attrs)?;
2180 match self.token.kind {
2181 token::Comma => {
2182 self.bump();
2183 }
2184 token::Semi => {
2185 self.bump();
2186 let sp = self.prev_token.span;
2187 let mut err =
2188 self.dcx().struct_span_err(sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} fields are separated by `,`",
adt_ty))
})format!("{adt_ty} fields are separated by `,`"));
2189 err.span_suggestion_short(
2190 sp,
2191 "replace `;` with `,`",
2192 ",",
2193 Applicability::MachineApplicable,
2194 );
2195 err.span_label(ident_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("while parsing this {0}", adt_ty))
})format!("while parsing this {adt_ty}"));
2196 err.emit();
2197 }
2198 token::CloseBrace => {}
2199 token::DocComment(..) => {
2200 let previous_span = self.prev_token.span;
2201 let mut err = errors::DocCommentDoesNotDocumentAnything {
2202 span: self.token.span,
2203 missing_comma: None,
2204 };
2205 self.bump(); if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma)) || self.token == token::CloseBrace {
2207 self.dcx().emit_err(err);
2208 } else {
2209 let sp = previous_span.shrink_to_hi();
2210 err.missing_comma = Some(sp);
2211 return Err(self.dcx().create_err(err));
2212 }
2213 }
2214 _ => {
2215 let sp = self.prev_token.span.shrink_to_hi();
2216 let msg =
2217 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `,`, or `}}`, found {0}",
super::token_descr(&self.token)))
})format!("expected `,`, or `}}`, found {}", super::token_descr(&self.token));
2218
2219 if let TyKind::Path(_, Path { segments, .. }) = &a_var.ty.kind
2221 && let Some(last_segment) = segments.last()
2222 {
2223 let guar = self.check_trailing_angle_brackets(
2224 last_segment,
2225 &[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseBrace,
token_type: crate::parser::token_type::TokenType::CloseBrace,
}exp!(CloseBrace)],
2226 );
2227 if let Some(_guar) = guar {
2228 let _ = self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma));
2231
2232 return Ok(a_var);
2235 }
2236 }
2237
2238 let mut err = self.dcx().struct_span_err(sp, msg);
2239
2240 if self.token.is_ident()
2241 || (self.token == TokenKind::Pound
2242 && (self.look_ahead(1, |t| t == &token::OpenBracket)))
2243 {
2244 err.span_suggestion(
2247 sp,
2248 "try adding a comma",
2249 ",",
2250 Applicability::MachineApplicable,
2251 );
2252 err.emit();
2253 } else {
2254 return Err(err);
2255 }
2256 }
2257 }
2258 Ok(a_var)
2259 }
2260
2261 fn expect_field_ty_separator(&mut self) -> PResult<'a, ()> {
2262 if let Err(err) = self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) {
2263 let sm = self.psess.source_map();
2264 let eq_typo = self.token == token::Eq && self.look_ahead(1, |t| t.is_path_start());
2265 let semi_typo = self.token == token::Semi
2266 && self.look_ahead(1, |t| {
2267 t.is_path_start()
2268 && match (sm.lookup_line(self.token.span.hi()), sm.lookup_line(t.span.lo())) {
2271 (Ok(l), Ok(r)) => l.line == r.line,
2272 _ => true,
2273 }
2274 });
2275 if eq_typo || semi_typo {
2276 self.bump();
2277 err.with_span_suggestion_short(
2279 self.prev_token.span,
2280 "field names and their types are separated with `:`",
2281 ":",
2282 Applicability::MachineApplicable,
2283 )
2284 .emit();
2285 } else {
2286 return Err(err);
2287 }
2288 }
2289 Ok(())
2290 }
2291
2292 fn parse_name_and_ty(
2294 &mut self,
2295 adt_ty: &str,
2296 lo: Span,
2297 vis: Visibility,
2298 safety: Safety,
2299 attrs: AttrVec,
2300 ) -> PResult<'a, FieldDef> {
2301 let name = self.parse_field_ident(adt_ty, lo)?;
2302 if self.token == token::Bang {
2303 if let Err(mut err) = self.unexpected() {
2304 err.subdiagnostic(MacroExpandsToAdtField { adt_ty });
2306 return Err(err);
2307 }
2308 }
2309 self.expect_field_ty_separator()?;
2310 let ty = self.parse_ty()?;
2311 if self.token == token::Colon && self.look_ahead(1, |&t| t != token::Colon) {
2312 self.dcx()
2313 .struct_span_err(self.token.span, "found single colon in a struct field type path")
2314 .with_span_suggestion_verbose(
2315 self.token.span,
2316 "write a path separator here",
2317 "::",
2318 Applicability::MaybeIncorrect,
2319 )
2320 .emit();
2321 }
2322 let default = if self.token == token::Eq {
2323 self.bump();
2324 let const_expr = self.parse_expr_anon_const(|_, _| MgcaDisambiguation::AnonConst)?;
2325 let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
2326 self.psess.gated_spans.gate(sym::default_field_values, sp);
2327 Some(const_expr)
2328 } else {
2329 None
2330 };
2331 Ok(FieldDef {
2332 span: lo.to(self.prev_token.span),
2333 ident: Some(name),
2334 vis,
2335 safety,
2336 id: DUMMY_NODE_ID,
2337 ty,
2338 default,
2339 attrs,
2340 is_placeholder: false,
2341 })
2342 }
2343
2344 fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
2347 let (ident, is_raw) = self.ident_or_err(true)?;
2348 if is_raw == IdentIsRaw::No && ident.is_reserved() {
2349 let snapshot = self.create_snapshot_for_diagnostic();
2350 let err = if self.check_fn_front_matter(false, Case::Sensitive) {
2351 let inherited_vis =
2352 Visibility { span: DUMMY_SP, kind: VisibilityKind::Inherited, tokens: None };
2353 let fn_parse_mode =
2355 FnParseMode { req_name: |_, _| true, context: FnContext::Free, req_body: true };
2356 match self.parse_fn(
2357 &mut AttrVec::new(),
2358 fn_parse_mode,
2359 lo,
2360 &inherited_vis,
2361 Case::Insensitive,
2362 ) {
2363 Ok(_) => {
2364 self.dcx().struct_span_err(
2365 lo.to(self.prev_token.span),
2366 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("functions are not allowed in {0} definitions",
adt_ty))
})format!("functions are not allowed in {adt_ty} definitions"),
2367 )
2368 .with_help(
2369 "unlike in C++, Java, and C#, functions are declared in `impl` blocks",
2370 )
2371 .with_help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information")
2372 }
2373 Err(err) => {
2374 err.cancel();
2375 self.restore_snapshot(snapshot);
2376 self.expected_ident_found_err()
2377 }
2378 }
2379 } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Struct,
token_type: crate::parser::token_type::TokenType::KwStruct,
}exp!(Struct)) {
2380 match self.parse_item_struct() {
2381 Ok(item) => {
2382 let ItemKind::Struct(ident, ..) = item else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
2383 self.dcx()
2384 .struct_span_err(
2385 lo.with_hi(ident.span.hi()),
2386 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("structs are not allowed in {0} definitions",
adt_ty))
})format!("structs are not allowed in {adt_ty} definitions"),
2387 )
2388 .with_help(
2389 "consider creating a new `struct` definition instead of nesting",
2390 )
2391 }
2392 Err(err) => {
2393 err.cancel();
2394 self.restore_snapshot(snapshot);
2395 self.expected_ident_found_err()
2396 }
2397 }
2398 } else {
2399 let mut err = self.expected_ident_found_err();
2400 if self.eat_keyword_noexpect(kw::Let)
2401 && let removal_span = self.prev_token.span.until(self.token.span)
2402 && let Ok(ident) = self
2403 .parse_ident_common(false)
2404 .map_err(|err| err.cancel())
2406 && self.token == TokenKind::Colon
2407 {
2408 err.span_suggestion(
2409 removal_span,
2410 "remove this `let` keyword",
2411 String::new(),
2412 Applicability::MachineApplicable,
2413 );
2414 err.note("the `let` keyword is not allowed in `struct` fields");
2415 err.note("see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information");
2416 err.emit();
2417 return Ok(ident);
2418 } else {
2419 self.restore_snapshot(snapshot);
2420 }
2421 err
2422 };
2423 return Err(err);
2424 }
2425 self.bump();
2426 Ok(ident)
2427 }
2428
2429 fn parse_item_decl_macro(&mut self, lo: Span) -> PResult<'a, ItemKind> {
2437 let ident = self.parse_ident()?;
2438 let body = if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
2439 self.parse_delim_args()? } else if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenParen,
token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
2441 let params = self.parse_token_tree(); let pspan = params.span();
2443 if !self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
2444 self.unexpected()?;
2445 }
2446 let body = self.parse_token_tree(); let bspan = body.span();
2449 let arrow = TokenTree::token_alone(token::FatArrow, pspan.between(bspan)); let tokens = TokenStream::new(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[params, arrow, body]))vec![params, arrow, body]);
2451 let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
2452 Box::new(DelimArgs { dspan, delim: Delimiter::Brace, tokens })
2453 } else {
2454 self.unexpected_any()?
2455 };
2456
2457 self.psess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_token.span));
2458 Ok(ItemKind::MacroDef(
2459 ident,
2460 ast::MacroDef { body, macro_rules: false, eii_declaration: None },
2461 ))
2462 }
2463
2464 fn is_macro_rules_item(&mut self) -> IsMacroRulesItem {
2466 if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::MacroRules,
token_type: crate::parser::token_type::TokenType::KwMacroRules,
}exp!(MacroRules)) {
2467 let macro_rules_span = self.token.span;
2468
2469 if self.look_ahead(1, |t| *t == token::Bang) && self.look_ahead(2, |t| t.is_ident()) {
2470 return IsMacroRulesItem::Yes { has_bang: true };
2471 } else if self.look_ahead(1, |t| t.is_ident()) {
2472 self.dcx().emit_err(errors::MacroRulesMissingBang {
2474 span: macro_rules_span,
2475 hi: macro_rules_span.shrink_to_hi(),
2476 });
2477
2478 return IsMacroRulesItem::Yes { has_bang: false };
2479 }
2480 }
2481
2482 IsMacroRulesItem::No
2483 }
2484
2485 fn parse_item_macro_rules(
2487 &mut self,
2488 vis: &Visibility,
2489 has_bang: bool,
2490 ) -> PResult<'a, ItemKind> {
2491 self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::MacroRules,
token_type: crate::parser::token_type::TokenType::KwMacroRules,
}exp!(MacroRules))?; if has_bang {
2494 self.expect(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang))?; }
2496 let ident = self.parse_ident()?;
2497
2498 if self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Bang,
token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) {
2499 let span = self.prev_token.span;
2501 self.dcx().emit_err(errors::MacroNameRemoveBang { span });
2502 }
2503
2504 let body = self.parse_delim_args()?;
2505 self.eat_semi_for_macro_if_needed(&body, None);
2506 self.complain_if_pub_macro(vis, true);
2507
2508 Ok(ItemKind::MacroDef(
2509 ident,
2510 ast::MacroDef { body, macro_rules: true, eii_declaration: None },
2511 ))
2512 }
2513
2514 fn complain_if_pub_macro(&self, vis: &Visibility, macro_rules: bool) {
2517 if let VisibilityKind::Inherited = vis.kind {
2518 return;
2519 }
2520
2521 let vstr = pprust::vis_to_string(vis);
2522 let vstr = vstr.trim_end();
2523 if macro_rules {
2524 self.dcx().emit_err(errors::MacroRulesVisibility { span: vis.span, vis: vstr });
2525 } else {
2526 self.dcx().emit_err(errors::MacroInvocationVisibility { span: vis.span, vis: vstr });
2527 }
2528 }
2529
2530 fn eat_semi_for_macro_if_needed(&mut self, args: &DelimArgs, path: Option<&Path>) {
2531 if args.need_semicolon() && !self.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
2532 self.report_invalid_macro_expansion_item(args, path);
2533 }
2534 }
2535
2536 fn report_invalid_macro_expansion_item(&self, args: &DelimArgs, path: Option<&Path>) {
2537 let span = args.dspan.entire();
2538 let mut err = self.dcx().struct_span_err(
2539 span,
2540 "macros that expand to items must be delimited with braces or followed by a semicolon",
2541 );
2542 if !span.from_expansion() {
2545 let DelimSpan { open, close } = args.dspan;
2546 if let Some(path) = path
2549 && path.segments.first().is_some_and(|seg| seg.ident.name == sym::macro_rules)
2550 && args.delim == Delimiter::Parenthesis
2551 {
2552 let replace =
2553 if path.span.hi() + rustc_span::BytePos(1) < open.lo() { "" } else { " " };
2554 err.multipart_suggestion(
2555 "to define a macro, remove the parentheses around the macro name",
2556 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(open, replace.to_string()), (close, String::new())]))vec![(open, replace.to_string()), (close, String::new())],
2557 Applicability::MachineApplicable,
2558 );
2559 } else {
2560 err.multipart_suggestion(
2561 "change the delimiters to curly braces",
2562 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(open, "{".to_string()), (close, '}'.to_string())]))vec![(open, "{".to_string()), (close, '}'.to_string())],
2563 Applicability::MaybeIncorrect,
2564 );
2565 err.span_suggestion(
2566 span.with_neighbor(self.token.span).shrink_to_hi(),
2567 "add a semicolon",
2568 ';',
2569 Applicability::MaybeIncorrect,
2570 );
2571 }
2572 }
2573 err.emit();
2574 }
2575
2576 fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
2579 if (self.token.is_keyword(kw::Enum)
2580 || self.token.is_keyword(kw::Struct)
2581 || self.token.is_keyword(kw::Union))
2582 && self.look_ahead(1, |t| t.is_ident())
2583 {
2584 let kw_token = self.token;
2585 let kw_str = pprust::token_to_string(&kw_token);
2586 let item = self.parse_item(
2587 ForceCollect::No,
2588 AllowConstBlockItems::DoesNotMatter, )?;
2590 let mut item = item.unwrap().span;
2591 if self.token == token::Comma {
2592 item = item.to(self.token.span);
2593 }
2594 self.dcx().emit_err(errors::NestedAdt {
2595 span: kw_token.span,
2596 item,
2597 kw_str,
2598 keyword: keyword.as_str(),
2599 });
2600 return Ok(false);
2602 }
2603 Ok(true)
2604 }
2605}
2606
2607type ReqName = fn(Edition, IsDotDotDot) -> bool;
2616
2617#[derive(#[automatically_derived]
impl ::core::marker::Copy for IsDotDotDot { }Copy, #[automatically_derived]
impl ::core::clone::Clone for IsDotDotDot {
#[inline]
fn clone(&self) -> IsDotDotDot { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for IsDotDotDot {
#[inline]
fn eq(&self, other: &IsDotDotDot) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
2618pub(crate) enum IsDotDotDot {
2619 Yes,
2620 No,
2621}
2622
2623#[derive(#[automatically_derived]
impl ::core::clone::Clone for FnParseMode {
#[inline]
fn clone(&self) -> FnParseMode {
let _: ::core::clone::AssertParamIsClone<ReqName>;
let _: ::core::clone::AssertParamIsClone<FnContext>;
let _: ::core::clone::AssertParamIsClone<bool>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for FnParseMode { }Copy)]
2631pub(crate) struct FnParseMode {
2632 pub(super) req_name: ReqName,
2658 pub(super) context: FnContext,
2661 pub(super) req_body: bool,
2680}
2681
2682#[derive(#[automatically_derived]
impl ::core::clone::Clone for FnContext {
#[inline]
fn clone(&self) -> FnContext { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for FnContext { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for FnContext {
#[inline]
fn eq(&self, other: &FnContext) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for FnContext {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq)]
2685pub(crate) enum FnContext {
2686 Free,
2688 Trait,
2690 Impl,
2692}
2693
2694impl<'a> Parser<'a> {
2696 fn parse_fn(
2698 &mut self,
2699 attrs: &mut AttrVec,
2700 fn_parse_mode: FnParseMode,
2701 sig_lo: Span,
2702 vis: &Visibility,
2703 case: Case,
2704 ) -> PResult<'a, (Ident, FnSig, Generics, Option<Box<FnContract>>, Option<Box<Block>>)> {
2705 let fn_span = self.token.span;
2706 let header = self.parse_fn_front_matter(vis, case, FrontMatterParsingMode::Function)?; let ident = self.parse_ident()?; let mut generics = self.parse_generics()?; let decl = match self.parse_fn_decl(&fn_parse_mode, AllowPlus::Yes, RecoverReturnSign::Yes)
2710 {
2711 Ok(decl) => decl,
2712 Err(old_err) => {
2713 if self.token.is_keyword(kw::For) {
2715 old_err.cancel();
2716 return Err(self.dcx().create_err(errors::FnTypoWithImpl { fn_span }));
2717 } else {
2718 return Err(old_err);
2719 }
2720 }
2721 };
2722
2723 let fn_params_end = self.prev_token.span.shrink_to_hi();
2726
2727 let contract = self.parse_contract()?;
2728
2729 generics.where_clause = self.parse_where_clause()?; let fn_params_end =
2733 if generics.where_clause.has_where_token { Some(fn_params_end) } else { None };
2734
2735 let mut sig_hi = self.prev_token.span;
2736 let body =
2738 self.parse_fn_body(attrs, &ident, &mut sig_hi, fn_parse_mode.req_body, fn_params_end)?;
2739 let fn_sig_span = sig_lo.to(sig_hi);
2740 Ok((ident, FnSig { header, decl, span: fn_sig_span }, generics, contract, body))
2741 }
2742
2743 fn error_fn_body_not_found(
2745 &mut self,
2746 ident_span: Span,
2747 req_body: bool,
2748 fn_params_end: Option<Span>,
2749 ) -> PResult<'a, ErrorGuaranteed> {
2750 let expected: &[_] =
2751 if req_body { &[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)] } else { &[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)] };
2752 match self.expected_one_of_not_found(&[], expected) {
2753 Ok(error_guaranteed) => Ok(error_guaranteed),
2754 Err(mut err) => {
2755 if self.token == token::CloseBrace {
2756 err.span_label(ident_span, "while parsing this `fn`");
2759 Ok(err.emit())
2760 } else if self.token == token::RArrow
2761 && let Some(fn_params_end) = fn_params_end
2762 {
2763 let fn_trait_span =
2769 [sym::FnOnce, sym::FnMut, sym::Fn].into_iter().find_map(|symbol| {
2770 if self.prev_token.is_ident_named(symbol) {
2771 Some(self.prev_token.span)
2772 } else {
2773 None
2774 }
2775 });
2776
2777 let arrow_span = self.token.span;
2782 let ty_span = match self.parse_ret_ty(
2783 AllowPlus::Yes,
2784 RecoverQPath::Yes,
2785 RecoverReturnSign::Yes,
2786 ) {
2787 Ok(ty_span) => ty_span.span().shrink_to_hi(),
2788 Err(parse_error) => {
2789 parse_error.cancel();
2790 return Err(err);
2791 }
2792 };
2793 let ret_ty_span = arrow_span.to(ty_span);
2794
2795 if let Some(fn_trait_span) = fn_trait_span {
2796 err.subdiagnostic(errors::FnTraitMissingParen { span: fn_trait_span });
2799 } else if let Ok(snippet) = self.psess.source_map().span_to_snippet(ret_ty_span)
2800 {
2801 err.primary_message(
2805 "return type should be specified after the function parameters",
2806 );
2807 err.subdiagnostic(errors::MisplacedReturnType {
2808 fn_params_end,
2809 snippet,
2810 ret_ty_span,
2811 });
2812 }
2813 Err(err)
2814 } else {
2815 Err(err)
2816 }
2817 }
2818 }
2819 }
2820
2821 fn parse_fn_body(
2825 &mut self,
2826 attrs: &mut AttrVec,
2827 ident: &Ident,
2828 sig_hi: &mut Span,
2829 req_body: bool,
2830 fn_params_end: Option<Span>,
2831 ) -> PResult<'a, Option<Box<Block>>> {
2832 let has_semi = if req_body {
2833 self.token == TokenKind::Semi
2834 } else {
2835 self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi))
2837 };
2838 let (inner_attrs, body) = if has_semi {
2839 self.expect_semi()?;
2841 *sig_hi = self.prev_token.span;
2842 (AttrVec::new(), None)
2843 } else if self.check(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::OpenBrace,
token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) || self.token.is_metavar_block() {
2844 let prev_in_fn_body = self.in_fn_body;
2845 self.in_fn_body = true;
2846 let res = self.parse_block_common(self.token.span, BlockCheckMode::Default, None).map(
2847 |(attrs, mut body)| {
2848 if let Some(guar) = self.fn_body_missing_semi_guar.take() {
2849 body.stmts.push(self.mk_stmt(
2850 body.span,
2851 StmtKind::Expr(self.mk_expr(body.span, ExprKind::Err(guar))),
2852 ));
2853 }
2854 (attrs, Some(body))
2855 },
2856 );
2857 self.in_fn_body = prev_in_fn_body;
2858 res?
2859 } else if self.token == token::Eq {
2860 self.bump(); let eq_sp = self.prev_token.span;
2863 let _ = self.parse_expr()?;
2864 self.expect_semi()?; let span = eq_sp.to(self.prev_token.span);
2866 let guar = self.dcx().emit_err(errors::FunctionBodyEqualsExpr {
2867 span,
2868 sugg: errors::FunctionBodyEqualsExprSugg { eq: eq_sp, semi: self.prev_token.span },
2869 });
2870 (AttrVec::new(), Some(self.mk_block_err(span, guar)))
2871 } else {
2872 self.error_fn_body_not_found(ident.span, req_body, fn_params_end)?;
2873 (AttrVec::new(), None)
2874 };
2875 attrs.extend(inner_attrs);
2876 Ok(body)
2877 }
2878
2879 fn check_impl_frontmatter(&mut self, look_ahead: usize) -> bool {
2880 const ALL_QUALS: &[Symbol] = &[kw::Const, kw::Unsafe];
2881 if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Impl,
token_type: crate::parser::token_type::TokenType::KwImpl,
}exp!(Impl)) {
2884 return true;
2885 }
2886 let mut i = 0;
2887 while i < ALL_QUALS.len() {
2888 let action = self.look_ahead(i + look_ahead, |token| {
2889 if token.is_keyword(kw::Impl) {
2890 return Some(true);
2891 }
2892 if ALL_QUALS.iter().any(|&qual| token.is_keyword(qual)) {
2893 return None;
2895 }
2896 Some(false)
2897 });
2898 if let Some(ret) = action {
2899 return ret;
2900 }
2901 i += 1;
2902 }
2903
2904 self.is_keyword_ahead(i, &[kw::Impl])
2905 }
2906
2907 pub(super) fn check_fn_front_matter(&mut self, check_pub: bool, case: Case) -> bool {
2912 const ALL_QUALS: &[ExpKeywordPair] = &[
2913 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Pub,
token_type: crate::parser::token_type::TokenType::KwPub,
}exp!(Pub),
2914 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Gen,
token_type: crate::parser::token_type::TokenType::KwGen,
}exp!(Gen),
2915 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const),
2916 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Async,
token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async),
2917 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe),
2918 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Safe,
token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe),
2919 crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern),
2920 ];
2921
2922 let quals: &[_] = if check_pub {
2927 ALL_QUALS
2928 } else {
2929 &[crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Gen,
token_type: crate::parser::token_type::TokenType::KwGen,
}exp!(Gen), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Async,
token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Safe,
token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe), crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern)]
2930 };
2931 self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Fn,
token_type: crate::parser::token_type::TokenType::KwFn,
}exp!(Fn), case) || quals.iter().any(|&exp| self.check_keyword_case(exp, case))
2934 && self.look_ahead(1, |t| {
2935 t.is_keyword_case(kw::Fn, case)
2937 || (
2939 (
2940 t.is_non_raw_ident_where(|i|
2941 quals.iter().any(|exp| exp.kw == i.name)
2942 && i.is_reserved()
2944 )
2945 || case == Case::Insensitive
2946 && t.is_non_raw_ident_where(|i| quals.iter().any(|exp| {
2947 exp.kw.as_str() == i.name.as_str().to_lowercase()
2948 }))
2949 )
2950 && !self.is_unsafe_foreign_mod()
2952 && !self.is_async_gen_block()
2954 && !self.is_keyword_ahead(2, &[kw::Auto, kw::Trait, kw::Impl])
2956 )
2957 })
2958 || self.check_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Extern,
token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern), case)
2960 && self.look_ahead(1, |t| t.can_begin_string_literal())
2964 && (self.tree_look_ahead(2, |tt| {
2965 match tt {
2966 TokenTree::Token(t, _) => t.is_keyword_case(kw::Fn, case),
2967 TokenTree::Delimited(..) => false,
2968 }
2969 }) == Some(true) ||
2970 (self.may_recover()
2973 && self.tree_look_ahead(2, |tt| {
2974 match tt {
2975 TokenTree::Token(t, _) =>
2976 ALL_QUALS.iter().any(|exp| {
2977 t.is_keyword(exp.kw)
2978 }),
2979 TokenTree::Delimited(..) => false,
2980 }
2981 }) == Some(true)
2982 && self.tree_look_ahead(3, |tt| {
2983 match tt {
2984 TokenTree::Token(t, _) => t.is_keyword_case(kw::Fn, case),
2985 TokenTree::Delimited(..) => false,
2986 }
2987 }) == Some(true)
2988 )
2989 )
2990 }
2991
2992 pub(super) fn parse_fn_front_matter(
3007 &mut self,
3008 orig_vis: &Visibility,
3009 case: Case,
3010 parsing_mode: FrontMatterParsingMode,
3011 ) -> PResult<'a, FnHeader> {
3012 let sp_start = self.token.span;
3013 let constness = self.parse_constness(case);
3014 if parsing_mode == FrontMatterParsingMode::FunctionPtrType
3015 && let Const::Yes(const_span) = constness
3016 {
3017 self.dcx().emit_err(FnPointerCannotBeConst {
3018 span: const_span,
3019 suggestion: const_span.until(self.token.span),
3020 });
3021 }
3022
3023 let async_start_sp = self.token.span;
3024 let coroutine_kind = self.parse_coroutine_kind(case);
3025 if parsing_mode == FrontMatterParsingMode::FunctionPtrType
3026 && let Some(ast::CoroutineKind::Async { span: async_span, .. }) = coroutine_kind
3027 {
3028 self.dcx().emit_err(FnPointerCannotBeAsync {
3029 span: async_span,
3030 suggestion: async_span.until(self.token.span),
3031 });
3032 }
3033 let unsafe_start_sp = self.token.span;
3036 let safety = self.parse_safety(case);
3037
3038 let ext_start_sp = self.token.span;
3039 let ext = self.parse_extern(case);
3040
3041 if let Some(CoroutineKind::Async { span, .. }) = coroutine_kind {
3042 if span.is_rust_2015() {
3043 self.dcx().emit_err(errors::AsyncFnIn2015 {
3044 span,
3045 help: errors::HelpUseLatestEdition::new(),
3046 });
3047 }
3048 }
3049
3050 match coroutine_kind {
3051 Some(CoroutineKind::Gen { span, .. }) | Some(CoroutineKind::AsyncGen { span, .. }) => {
3052 self.psess.gated_spans.gate(sym::gen_blocks, span);
3053 }
3054 Some(CoroutineKind::Async { .. }) | None => {}
3055 }
3056
3057 if !self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Fn,
token_type: crate::parser::token_type::TokenType::KwFn,
}exp!(Fn), case) {
3058 match self.expect_one_of(&[], &[]) {
3062 Ok(Recovered::Yes(_)) => {}
3063 Ok(Recovered::No) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
3064 Err(mut err) => {
3065 enum WrongKw {
3067 Duplicated(Span),
3068 Misplaced(Span),
3069 MisplacedDisallowedQualifier,
3074 }
3075
3076 let mut recover_constness = constness;
3078 let mut recover_coroutine_kind = coroutine_kind;
3079 let mut recover_safety = safety;
3080 let wrong_kw = if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Const,
token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const)) {
3083 match constness {
3084 Const::Yes(sp) => Some(WrongKw::Duplicated(sp)),
3085 Const::No => {
3086 recover_constness = Const::Yes(self.token.span);
3087 match parsing_mode {
3088 FrontMatterParsingMode::Function => {
3089 Some(WrongKw::Misplaced(async_start_sp))
3090 }
3091 FrontMatterParsingMode::FunctionPtrType => {
3092 self.dcx().emit_err(FnPointerCannotBeConst {
3093 span: self.token.span,
3094 suggestion: self
3095 .token
3096 .span
3097 .with_lo(self.prev_token.span.hi()),
3098 });
3099 Some(WrongKw::MisplacedDisallowedQualifier)
3100 }
3101 }
3102 }
3103 }
3104 } else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Async,
token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async)) {
3105 match coroutine_kind {
3106 Some(CoroutineKind::Async { span, .. }) => {
3107 Some(WrongKw::Duplicated(span))
3108 }
3109 Some(CoroutineKind::AsyncGen { span, .. }) => {
3110 Some(WrongKw::Duplicated(span))
3111 }
3112 Some(CoroutineKind::Gen { .. }) => {
3113 recover_coroutine_kind = Some(CoroutineKind::AsyncGen {
3114 span: self.token.span,
3115 closure_id: DUMMY_NODE_ID,
3116 return_impl_trait_id: DUMMY_NODE_ID,
3117 });
3118 Some(WrongKw::Misplaced(unsafe_start_sp))
3120 }
3121 None => {
3122 recover_coroutine_kind = Some(CoroutineKind::Async {
3123 span: self.token.span,
3124 closure_id: DUMMY_NODE_ID,
3125 return_impl_trait_id: DUMMY_NODE_ID,
3126 });
3127 match parsing_mode {
3128 FrontMatterParsingMode::Function => {
3129 Some(WrongKw::Misplaced(async_start_sp))
3130 }
3131 FrontMatterParsingMode::FunctionPtrType => {
3132 self.dcx().emit_err(FnPointerCannotBeAsync {
3133 span: self.token.span,
3134 suggestion: self
3135 .token
3136 .span
3137 .with_lo(self.prev_token.span.hi()),
3138 });
3139 Some(WrongKw::MisplacedDisallowedQualifier)
3140 }
3141 }
3142 }
3143 }
3144 } else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Unsafe,
token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe)) {
3145 match safety {
3146 Safety::Unsafe(sp) => Some(WrongKw::Duplicated(sp)),
3147 Safety::Safe(sp) => {
3148 recover_safety = Safety::Unsafe(self.token.span);
3149 Some(WrongKw::Misplaced(sp))
3150 }
3151 Safety::Default => {
3152 recover_safety = Safety::Unsafe(self.token.span);
3153 Some(WrongKw::Misplaced(ext_start_sp))
3154 }
3155 }
3156 } else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Safe,
token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe)) {
3157 match safety {
3158 Safety::Safe(sp) => Some(WrongKw::Duplicated(sp)),
3159 Safety::Unsafe(sp) => {
3160 recover_safety = Safety::Safe(self.token.span);
3161 Some(WrongKw::Misplaced(sp))
3162 }
3163 Safety::Default => {
3164 recover_safety = Safety::Safe(self.token.span);
3165 Some(WrongKw::Misplaced(ext_start_sp))
3166 }
3167 }
3168 } else {
3169 None
3170 };
3171
3172 if let Some(WrongKw::Duplicated(original_sp)) = wrong_kw {
3174 let original_kw = self
3175 .span_to_snippet(original_sp)
3176 .expect("Span extracted directly from keyword should always work");
3177
3178 err.span_suggestion(
3179 self.token_uninterpolated_span(),
3180 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` already used earlier, remove this one",
original_kw))
})format!("`{original_kw}` already used earlier, remove this one"),
3181 "",
3182 Applicability::MachineApplicable,
3183 )
3184 .span_note(original_sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` first seen here",
original_kw))
})format!("`{original_kw}` first seen here"));
3185 }
3186 else if let Some(WrongKw::Misplaced(correct_pos_sp)) = wrong_kw {
3188 let correct_pos_sp = correct_pos_sp.to(self.prev_token.span);
3189 if let Ok(current_qual) = self.span_to_snippet(correct_pos_sp) {
3190 let misplaced_qual_sp = self.token_uninterpolated_span();
3191 let misplaced_qual = self.span_to_snippet(misplaced_qual_sp).unwrap();
3192
3193 err.span_suggestion(
3194 correct_pos_sp.to(misplaced_qual_sp),
3195 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` must come before `{1}`",
misplaced_qual, current_qual))
})format!("`{misplaced_qual}` must come before `{current_qual}`"),
3196 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1}", misplaced_qual,
current_qual))
})format!("{misplaced_qual} {current_qual}"),
3197 Applicability::MachineApplicable,
3198 ).note("keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern`");
3199 }
3200 }
3201 else if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
kw: rustc_span::symbol::kw::Pub,
token_type: crate::parser::token_type::TokenType::KwPub,
}exp!(Pub)) {
3203 let sp = sp_start.to(self.prev_token.span);
3204 if let Ok(snippet) = self.span_to_snippet(sp) {
3205 let current_vis = match self.parse_visibility(FollowedByType::No) {
3206 Ok(v) => v,
3207 Err(d) => {
3208 d.cancel();
3209 return Err(err);
3210 }
3211 };
3212 let vs = pprust::vis_to_string(¤t_vis);
3213 let vs = vs.trim_end();
3214
3215 if #[allow(non_exhaustive_omitted_patterns)] match orig_vis.kind {
VisibilityKind::Inherited => true,
_ => false,
}matches!(orig_vis.kind, VisibilityKind::Inherited) {
3217 err.span_suggestion(
3218 sp_start.to(self.prev_token.span),
3219 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("visibility `{0}` must come before `{1}`",
vs, snippet))
})format!("visibility `{vs}` must come before `{snippet}`"),
3220 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} {1}", vs, snippet))
})format!("{vs} {snippet}"),
3221 Applicability::MachineApplicable,
3222 );
3223 }
3224 else {
3226 err.span_suggestion(
3227 current_vis.span,
3228 "there is already a visibility modifier, remove one",
3229 "",
3230 Applicability::MachineApplicable,
3231 )
3232 .span_note(orig_vis.span, "explicit visibility first seen here");
3233 }
3234 }
3235 }
3236
3237 if let Some(wrong_kw) = wrong_kw
3240 && self.may_recover()
3241 && self.look_ahead(1, |tok| tok.is_keyword_case(kw::Fn, case))
3242 {
3243 self.bump();
3245 self.bump();
3246 if #[allow(non_exhaustive_omitted_patterns)] match wrong_kw {
WrongKw::MisplacedDisallowedQualifier => true,
_ => false,
}matches!(wrong_kw, WrongKw::MisplacedDisallowedQualifier) {
3249 err.cancel();
3250 } else {
3251 err.emit();
3252 }
3253 return Ok(FnHeader {
3254 constness: recover_constness,
3255 safety: recover_safety,
3256 coroutine_kind: recover_coroutine_kind,
3257 ext,
3258 });
3259 }
3260
3261 return Err(err);
3262 }
3263 }
3264 }
3265
3266 Ok(FnHeader { constness, safety, coroutine_kind, ext })
3267 }
3268
3269 pub(super) fn parse_fn_decl(
3271 &mut self,
3272 fn_parse_mode: &FnParseMode,
3273 ret_allow_plus: AllowPlus,
3274 recover_return_sign: RecoverReturnSign,
3275 ) -> PResult<'a, Box<FnDecl>> {
3276 Ok(Box::new(FnDecl {
3277 inputs: self.parse_fn_params(fn_parse_mode)?,
3278 output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes, recover_return_sign)?,
3279 }))
3280 }
3281
3282 pub(super) fn parse_fn_params(
3284 &mut self,
3285 fn_parse_mode: &FnParseMode,
3286 ) -> PResult<'a, ThinVec<Param>> {
3287 let mut first_param = true;
3288 if self.token != TokenKind::OpenParen
3290 && !self.token.is_keyword(kw::For)
3292 {
3293 self.dcx()
3295 .emit_err(errors::MissingFnParams { span: self.prev_token.span.shrink_to_hi() });
3296 return Ok(ThinVec::new());
3297 }
3298
3299 let (mut params, _) = self.parse_paren_comma_seq(|p| {
3300 p.recover_vcs_conflict_marker();
3301 let snapshot = p.create_snapshot_for_diagnostic();
3302 let param = p.parse_param_general(fn_parse_mode, first_param, true).or_else(|e| {
3303 let guar = e.emit();
3304 let lo = if let TokenKind::OpenParen = p.prev_token.kind {
3308 p.prev_token.span.shrink_to_hi()
3309 } else {
3310 p.prev_token.span
3311 };
3312 p.restore_snapshot(snapshot);
3313 p.eat_to_tokens(&[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma), crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen)]);
3315 Ok(dummy_arg(Ident::new(sym::dummy, lo.to(p.prev_token.span)), guar))
3317 });
3318 first_param = false;
3320 param
3321 })?;
3322 self.deduplicate_recovered_params_names(&mut params);
3324 Ok(params)
3325 }
3326
3327 pub(super) fn parse_param_general(
3332 &mut self,
3333 fn_parse_mode: &FnParseMode,
3334 first_param: bool,
3335 recover_arg_parse: bool,
3336 ) -> PResult<'a, Param> {
3337 let lo = self.token.span;
3338 let attrs = self.parse_outer_attributes()?;
3339 self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
3340 if let Some(mut param) = this.parse_self_param()? {
3342 param.attrs = attrs;
3343 let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) };
3344 return Ok((res?, Trailing::No, UsePreAttrPos::No));
3345 }
3346
3347 let is_dot_dot_dot = if this.token.kind == token::DotDotDot {
3348 IsDotDotDot::Yes
3349 } else {
3350 IsDotDotDot::No
3351 };
3352 let is_name_required = (fn_parse_mode.req_name)(
3353 this.token.span.with_neighbor(this.prev_token.span).edition(),
3354 is_dot_dot_dot,
3355 );
3356 let is_name_required = if is_name_required && is_dot_dot_dot == IsDotDotDot::Yes {
3357 this.psess.buffer_lint(
3358 VARARGS_WITHOUT_PATTERN,
3359 this.token.span,
3360 ast::CRATE_NODE_ID,
3361 errors::VarargsWithoutPattern { span: this.token.span },
3362 );
3363 false
3364 } else {
3365 is_name_required
3366 };
3367 let (pat, ty) = if is_name_required || this.is_named_param() {
3368 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/item.rs:3368",
"rustc_parse::parser::item", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/item.rs"),
::tracing_core::__macro_support::Option::Some(3368u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::parser::item"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("parse_param_general parse_pat (is_name_required:{0})",
is_name_required) as &dyn Value))])
});
} else { ; }
};debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
3369 let (pat, colon) = this.parse_fn_param_pat_colon()?;
3370 if !colon {
3371 let mut err = this.unexpected().unwrap_err();
3372 return if let Some(ident) = this.parameter_without_type(
3373 &mut err,
3374 pat,
3375 is_name_required,
3376 first_param,
3377 fn_parse_mode,
3378 ) {
3379 let guar = err.emit();
3380 Ok((dummy_arg(ident, guar), Trailing::No, UsePreAttrPos::No))
3381 } else {
3382 Err(err)
3383 };
3384 }
3385
3386 this.eat_incorrect_doc_comment_for_param_type();
3387 (pat, this.parse_ty_for_param()?)
3388 } else {
3389 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/item.rs:3389",
"rustc_parse::parser::item", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/item.rs"),
::tracing_core::__macro_support::Option::Some(3389u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::parser::item"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("parse_param_general ident_to_pat")
as &dyn Value))])
});
} else { ; }
};debug!("parse_param_general ident_to_pat");
3390 let parser_snapshot_before_ty = this.create_snapshot_for_diagnostic();
3391 this.eat_incorrect_doc_comment_for_param_type();
3392 let mut ty = this.parse_ty_for_param();
3393
3394 if let Ok(t) = &ty {
3395 if let TyKind::Path(_, Path { segments, .. }) = &t.kind
3397 && let Some(segment) = segments.last()
3398 && let Some(guar) =
3399 this.check_trailing_angle_brackets(segment, &[crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::CloseParen,
token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen)])
3400 {
3401 return Ok((
3402 dummy_arg(segment.ident, guar),
3403 Trailing::No,
3404 UsePreAttrPos::No,
3405 ));
3406 }
3407
3408 if this.token != token::Comma && this.token != token::CloseParen {
3409 ty = this.unexpected_any();
3412 }
3413 }
3414 match ty {
3415 Ok(ty) => {
3416 let pat = this.mk_pat(ty.span, PatKind::Missing);
3417 (Box::new(pat), ty)
3418 }
3419 Err(err) if this.token == token::DotDotDot => return Err(err),
3421 Err(err) if this.unmatched_angle_bracket_count > 0 => return Err(err),
3422 Err(err) if recover_arg_parse => {
3423 err.cancel();
3425 this.restore_snapshot(parser_snapshot_before_ty);
3426 this.recover_arg_parse()?
3427 }
3428 Err(err) => return Err(err),
3429 }
3430 };
3431
3432 let span = lo.to(this.prev_token.span);
3433
3434 Ok((
3435 Param { attrs, id: ast::DUMMY_NODE_ID, is_placeholder: false, pat, span, ty },
3436 Trailing::No,
3437 UsePreAttrPos::No,
3438 ))
3439 })
3440 }
3441
3442 fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
3444 let expect_self_ident = |this: &mut Self| match this.token.ident() {
3446 Some((ident, IdentIsRaw::No)) => {
3447 this.bump();
3448 ident
3449 }
3450 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
3451 };
3452 let is_lifetime = |this: &Self, n| this.look_ahead(n, |t| t.is_lifetime());
3454 let is_isolated_self = |this: &Self, n| {
3456 this.is_keyword_ahead(n, &[kw::SelfLower])
3457 && this.look_ahead(n + 1, |t| t != &token::PathSep)
3458 };
3459 let is_isolated_pin_const_self = |this: &Self, n| {
3461 this.look_ahead(n, |token| token.is_ident_named(sym::pin))
3462 && this.is_keyword_ahead(n + 1, &[kw::Const])
3463 && is_isolated_self(this, n + 2)
3464 };
3465 let is_isolated_mut_self =
3467 |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
3468 let is_isolated_pin_mut_self = |this: &Self, n| {
3470 this.look_ahead(n, |token| token.is_ident_named(sym::pin))
3471 && is_isolated_mut_self(this, n + 1)
3472 };
3473 let parse_self_possibly_typed = |this: &mut Self, m| {
3475 let eself_ident = expect_self_ident(this);
3476 let eself_hi = this.prev_token.span;
3477 let eself = if this.eat(crate::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Colon,
token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon)) {
3478 SelfKind::Explicit(this.parse_ty()?, m)
3479 } else {
3480 SelfKind::Value(m)
3481 };
3482 Ok((eself, eself_ident, eself_hi))
3483 };
3484 let expect_self_ident_not_typed =
3485 |this: &mut Self, modifier: &SelfKind, modifier_span: Span| {
3486 let eself_ident = expect_self_ident(this);
3487
3488 if this.may_recover() && this.eat_noexpect(&token::Colon) {
3490 let snap = this.create_snapshot_for_diagnostic();
3491 match this.parse_ty() {
3492 Ok(ty) => {
3493 this.dcx().emit_err(errors::IncorrectTypeOnSelf {
3494 span: ty.span,
3495 move_self_modifier: errors::MoveSelfModifier {
3496 removal_span: modifier_span,
3497 insertion_span: ty.span.shrink_to_lo(),
3498 modifier: modifier.to_ref_suggestion(),
3499 },
3500 });
3501 }
3502 Err(diag) => {
3503 diag.cancel();
3504 this.restore_snapshot(snap);
3505 }
3506 }
3507 }
3508 eself_ident
3509 };
3510 let recover_self_ptr = |this: &mut Self| {
3512 this.dcx().emit_err(errors::SelfArgumentPointer { span: this.token.span });
3513
3514 Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_token.span))
3515 };
3516
3517 let eself_lo = self.token.span;
3521 let (eself, eself_ident, eself_hi) = match self.token.uninterpolate().kind {
3522 token::And => {
3523 let has_lifetime = is_lifetime(self, 1);
3524 let skip_lifetime_count = has_lifetime as usize;
3525 let eself = if is_isolated_self(self, skip_lifetime_count + 1) {
3526 self.bump(); let lifetime = has_lifetime.then(|| self.expect_lifetime());
3529 SelfKind::Region(lifetime, Mutability::Not)
3530 } else if is_isolated_mut_self(self, skip_lifetime_count + 1) {
3531 self.bump(); let lifetime = has_lifetime.then(|| self.expect_lifetime());
3534 self.bump(); SelfKind::Region(lifetime, Mutability::Mut)
3536 } else if is_isolated_pin_const_self(self, skip_lifetime_count + 1) {
3537 self.bump(); let lifetime = has_lifetime.then(|| self.expect_lifetime());
3540 self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3541 self.bump(); self.bump(); SelfKind::Pinned(lifetime, Mutability::Not)
3544 } else if is_isolated_pin_mut_self(self, skip_lifetime_count + 1) {
3545 self.bump(); let lifetime = has_lifetime.then(|| self.expect_lifetime());
3548 self.psess.gated_spans.gate(sym::pin_ergonomics, self.token.span);
3549 self.bump(); self.bump(); SelfKind::Pinned(lifetime, Mutability::Mut)
3552 } else {
3553 return Ok(None);
3555 };
3556 let hi = self.token.span;
3557 let self_ident = expect_self_ident_not_typed(self, &eself, eself_lo.until(hi));
3558 (eself, self_ident, hi)
3559 }
3560 token::Star if is_isolated_self(self, 1) => {
3562 self.bump();
3563 recover_self_ptr(self)?
3564 }
3565 token::Star
3567 if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
3568 {
3569 self.bump();
3570 self.bump();
3571 recover_self_ptr(self)?
3572 }
3573 token::Ident(..) if is_isolated_self(self, 0) => {
3575 parse_self_possibly_typed(self, Mutability::Not)?
3576 }
3577 token::Ident(..) if is_isolated_mut_self(self, 0) => {
3579 self.bump();
3580 parse_self_possibly_typed(self, Mutability::Mut)?
3581 }
3582 _ => return Ok(None),
3583 };
3584
3585 let eself = respan(eself_lo.to(eself_hi), eself);
3586 Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
3587 }
3588
3589 fn is_named_param(&self) -> bool {
3590 let offset = match &self.token.kind {
3591 token::OpenInvisible(origin) => match origin {
3592 InvisibleOrigin::MetaVar(MetaVarKind::Pat(_)) => {
3593 return self.check_noexpect_past_close_delim(&token::Colon);
3594 }
3595 _ => 0,
3596 },
3597 token::And | token::AndAnd => 1,
3598 _ if self.token.is_keyword(kw::Mut) => 1,
3599 _ => 0,
3600 };
3601
3602 self.look_ahead(offset, |t| t.is_ident())
3603 && self.look_ahead(offset + 1, |t| t == &token::Colon)
3604 }
3605
3606 fn recover_self_param(&mut self) -> bool {
3607 #[allow(non_exhaustive_omitted_patterns)] match self.parse_outer_attributes().and_then(|_|
self.parse_self_param()).map_err(|e| e.cancel()) {
Ok(Some(_)) => true,
_ => false,
}matches!(
3608 self.parse_outer_attributes()
3609 .and_then(|_| self.parse_self_param())
3610 .map_err(|e| e.cancel()),
3611 Ok(Some(_))
3612 )
3613 }
3614
3615 fn try_recover_const_missing_semi(
3623 &mut self,
3624 rhs: &ConstItemRhsKind,
3625 const_span: Span,
3626 ) -> Option<Box<Expr>> {
3627 if self.token == TokenKind::Semi {
3628 return None;
3629 }
3630 let ConstItemRhsKind::Body { rhs: Some(rhs) } = rhs else {
3631 return None;
3632 };
3633 if !self.in_fn_body || !self.may_recover() || rhs.span.from_expansion() {
3634 return None;
3635 }
3636 if let Some((span, guar)) =
3637 self.missing_semi_from_binop("const", rhs, Some(const_span.shrink_to_lo()))
3638 {
3639 self.fn_body_missing_semi_guar = Some(guar);
3640 Some(self.mk_expr(span, ExprKind::Err(guar)))
3641 } else {
3642 None
3643 }
3644 }
3645}
3646
3647enum IsMacroRulesItem {
3648 Yes { has_bang: bool },
3649 No,
3650}
3651
3652#[derive(#[automatically_derived]
impl ::core::marker::Copy for FrontMatterParsingMode { }Copy, #[automatically_derived]
impl ::core::clone::Clone for FrontMatterParsingMode {
#[inline]
fn clone(&self) -> FrontMatterParsingMode { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for FrontMatterParsingMode {
#[inline]
fn eq(&self, other: &FrontMatterParsingMode) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for FrontMatterParsingMode {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq)]
3653pub(super) enum FrontMatterParsingMode {
3654 Function,
3656 FunctionPtrType,
3659}