2019-02-08 13:53:55 +00:00
|
|
|
//! The main parser interface.
|
2012-11-29 00:20:41 +00:00
|
|
|
|
Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.
For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
`allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
sometimes the order is alphabetical, and sometimes there is no
particular order.
- Sometimes the attributes of a particular kind aren't even grouped
all together, e.g. there might be a `feature`, then an `allow`, then
another `feature`.
This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.
Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
ignored in `rustfmt.toml`).
2024-06-12 03:49:36 +00:00
|
|
|
// tidy-alphabetical-start
|
2024-02-05 22:51:39 +00:00
|
|
|
#![allow(internal_features)]
|
|
|
|
#![allow(rustc::diagnostic_outside_of_impl)]
|
|
|
|
#![allow(rustc::untranslatable_diagnostic)]
|
2020-11-28 23:33:17 +00:00
|
|
|
#![feature(array_windows)]
|
2022-02-28 10:49:56 +00:00
|
|
|
#![feature(box_patterns)]
|
2024-05-05 22:41:00 +00:00
|
|
|
#![feature(debug_closure_helpers)]
|
2021-08-16 15:29:49 +00:00
|
|
|
#![feature(if_let_guard)]
|
2022-11-10 01:06:11 +00:00
|
|
|
#![feature(iter_intersperse)]
|
2022-08-20 18:40:08 +00:00
|
|
|
#![feature(let_chains)]
|
Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.
For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
`allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
sometimes the order is alphabetical, and sometimes there is no
particular order.
- Sometimes the attributes of a particular kind aren't even grouped
all together, e.g. there might be a `feature`, then an `allow`, then
another `feature`.
This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.
Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
ignored in `rustfmt.toml`).
2024-06-12 03:49:36 +00:00
|
|
|
// tidy-alphabetical-end
|
2019-10-15 20:48:13 +00:00
|
|
|
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2022-05-21 12:50:39 +00:00
|
|
|
use rustc_ast::token;
|
|
|
|
use rustc_ast::tokenstream::TokenStream;
|
2022-11-18 00:24:21 +00:00
|
|
|
use rustc_ast::{AttrItem, Attribute, MetaItem};
|
2022-05-21 12:50:39 +00:00
|
|
|
use rustc_ast_pretty::pprust;
|
2020-01-11 14:03:15 +00:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2024-02-22 23:20:45 +00:00
|
|
|
use rustc_errors::{Diag, FatalError, PResult};
|
2020-01-11 14:03:15 +00:00
|
|
|
use rustc_session::parse::ParseSess;
|
2020-11-23 06:43:55 +00:00
|
|
|
use rustc_span::{FileName, SourceFile, Span};
|
2019-02-06 17:33:01 +00:00
|
|
|
|
2020-03-08 21:10:37 +00:00
|
|
|
use std::path::Path;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2020-10-18 15:28:00 +00:00
|
|
|
pub const MACRO_ARGUMENTS: Option<&str> = Some("macro arguments");
|
2015-03-28 21:58:51 +00:00
|
|
|
|
2015-01-06 17:24:46 +00:00
|
|
|
#[macro_use]
|
2013-01-09 03:37:25 +00:00
|
|
|
pub mod parser;
|
2023-02-22 06:09:57 +00:00
|
|
|
use parser::{make_unclosed_delims_error, Parser};
|
2014-12-19 04:09:57 +00:00
|
|
|
pub mod lexer;
|
2019-10-11 19:00:09 +00:00
|
|
|
pub mod validate_attr;
|
2019-04-25 08:48:25 +00:00
|
|
|
|
2022-08-30 11:19:17 +00:00
|
|
|
mod errors;
|
|
|
|
|
2023-11-21 22:53:07 +00:00
|
|
|
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
|
2022-10-13 09:13:02 +00:00
|
|
|
|
2024-05-31 03:36:18 +00:00
|
|
|
// Unwrap the result if `Ok`, otherwise emit the diagnostics and abort.
|
2024-05-31 05:43:18 +00:00
|
|
|
pub fn unwrap_or_emit_fatal<T>(expr: Result<T, Vec<Diag<'_>>>) -> T {
|
2024-05-31 03:36:18 +00:00
|
|
|
match expr {
|
|
|
|
Ok(expr) => expr,
|
|
|
|
Err(errs) => {
|
|
|
|
for err in errs {
|
|
|
|
err.emit();
|
2019-10-11 16:52:09 +00:00
|
|
|
}
|
2024-05-31 03:36:18 +00:00
|
|
|
FatalError.raise()
|
2019-10-11 16:52:09 +00:00
|
|
|
}
|
2024-05-31 03:36:18 +00:00
|
|
|
}
|
2019-10-11 16:52:09 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 05:43:18 +00:00
|
|
|
/// Creates a new parser from a source string. On failure, the errors must be consumed via
|
|
|
|
/// `unwrap_or_emit_fatal`, `emit`, `cancel`, etc., otherwise a panic will occur when they are
|
|
|
|
/// dropped.
|
|
|
|
pub fn new_parser_from_source_str(
|
2024-03-04 05:31:49 +00:00
|
|
|
psess: &ParseSess,
|
2018-11-01 16:57:29 +00:00
|
|
|
name: FileName,
|
|
|
|
source: String,
|
2024-02-22 23:20:45 +00:00
|
|
|
) -> Result<Parser<'_>, Vec<Diag<'_>>> {
|
2024-05-31 05:43:18 +00:00
|
|
|
let source_file = psess.source_map().new_source_file(name, source);
|
|
|
|
new_parser_from_source_file(psess, source_file)
|
2012-11-29 00:20:41 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 05:43:18 +00:00
|
|
|
/// Creates a new parser from a filename. On failure, the errors must be consumed via
|
|
|
|
/// `unwrap_or_emit_fatal`, `emit`, `cancel`, etc., otherwise a panic will occur when they are
|
|
|
|
/// dropped.
|
|
|
|
///
|
|
|
|
/// If a span is given, that is used on an error as the source of the problem.
|
|
|
|
pub fn new_parser_from_file<'a>(
|
|
|
|
psess: &'a ParseSess,
|
|
|
|
path: &Path,
|
|
|
|
sp: Option<Span>,
|
|
|
|
) -> Result<Parser<'a>, Vec<Diag<'a>>> {
|
2024-03-04 05:31:49 +00:00
|
|
|
let source_file = psess.source_map().load_file(path).unwrap_or_else(|e| {
|
2024-01-11 02:50:00 +00:00
|
|
|
let msg = format!("couldn't read {}: {}", path.display(), e);
|
2024-06-18 09:43:28 +00:00
|
|
|
let mut err = psess.dcx().struct_fatal(msg);
|
2024-01-11 02:50:00 +00:00
|
|
|
if let Some(sp) = sp {
|
2024-01-11 05:24:31 +00:00
|
|
|
err.span(sp);
|
2024-01-11 02:50:00 +00:00
|
|
|
}
|
2024-01-11 05:24:31 +00:00
|
|
|
err.emit();
|
2024-01-11 02:50:00 +00:00
|
|
|
});
|
2024-05-31 05:43:18 +00:00
|
|
|
new_parser_from_source_file(psess, source_file)
|
2013-04-23 17:57:41 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 02:50:00 +00:00
|
|
|
/// Given a session and a `source_file`, return a parser. Returns any buffered errors from lexing
|
|
|
|
/// the initial token stream.
|
2024-05-31 05:43:18 +00:00
|
|
|
fn new_parser_from_source_file(
|
2024-03-04 05:31:49 +00:00
|
|
|
psess: &ParseSess,
|
2019-01-28 05:04:50 +00:00
|
|
|
source_file: Lrc<SourceFile>,
|
2024-02-22 23:20:45 +00:00
|
|
|
) -> Result<Parser<'_>, Vec<Diag<'_>>> {
|
2023-09-03 10:15:35 +00:00
|
|
|
let end_pos = source_file.end_position();
|
2024-05-31 05:43:18 +00:00
|
|
|
let stream = source_file_to_stream(psess, source_file, None)?;
|
2024-05-31 03:32:54 +00:00
|
|
|
let mut parser = Parser::new(psess, stream, None);
|
2020-02-16 20:19:51 +00:00
|
|
|
if parser.token == token::Eof {
|
2021-04-18 12:27:04 +00:00
|
|
|
parser.token.span = Span::new(end_pos, end_pos, parser.token.span.ctxt(), None);
|
2015-07-06 02:13:19 +00:00
|
|
|
}
|
2018-11-01 16:57:29 +00:00
|
|
|
Ok(parser)
|
2013-04-23 17:57:41 +00:00
|
|
|
}
|
|
|
|
|
2024-05-30 23:23:35 +00:00
|
|
|
pub fn source_str_to_stream(
|
2024-05-31 04:33:56 +00:00
|
|
|
psess: &ParseSess,
|
2024-05-30 23:23:35 +00:00
|
|
|
name: FileName,
|
|
|
|
source: String,
|
|
|
|
override_span: Option<Span>,
|
2024-05-31 05:43:18 +00:00
|
|
|
) -> Result<TokenStream, Vec<Diag<'_>>> {
|
2024-05-31 04:38:34 +00:00
|
|
|
let source_file = psess.source_map().new_source_file(name, source);
|
2024-05-31 05:43:18 +00:00
|
|
|
source_file_to_stream(psess, source_file, override_span)
|
2013-04-23 17:57:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Given a source file, produces a sequence of token trees. Returns any buffered errors from
|
2019-05-12 16:55:16 +00:00
|
|
|
/// parsing the token stream.
|
2024-05-31 05:43:18 +00:00
|
|
|
fn source_file_to_stream<'psess>(
|
2024-03-04 05:31:49 +00:00
|
|
|
psess: &'psess ParseSess,
|
2019-01-28 05:04:50 +00:00
|
|
|
source_file: Lrc<SourceFile>,
|
|
|
|
override_span: Option<Span>,
|
2024-03-04 05:31:49 +00:00
|
|
|
) -> Result<TokenStream, Vec<Diag<'psess>>> {
|
2020-09-03 13:37:03 +00:00
|
|
|
let src = source_file.src.as_ref().unwrap_or_else(|| {
|
2024-06-18 09:43:28 +00:00
|
|
|
psess.dcx().bug(format!(
|
2021-04-19 22:27:02 +00:00
|
|
|
"cannot lex `source_file` without source: {}",
|
2024-03-04 05:31:49 +00:00
|
|
|
psess.source_map().filename_for_diagnostics(&source_file.name)
|
2021-04-19 22:27:02 +00:00
|
|
|
));
|
2020-09-03 13:37:03 +00:00
|
|
|
});
|
|
|
|
|
2024-05-30 23:23:35 +00:00
|
|
|
lexer::lex_token_trees(psess, src.as_str(), source_file.start_pos, override_span)
|
2013-04-23 17:57:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 11:19:15 +00:00
|
|
|
/// Runs the given subparser `f` on the tokens of the given `attr`'s item.
|
2019-12-05 05:45:50 +00:00
|
|
|
pub fn parse_in<'a, T>(
|
2024-03-04 05:31:49 +00:00
|
|
|
psess: &'a ParseSess,
|
2019-12-05 05:45:50 +00:00
|
|
|
tts: TokenStream,
|
|
|
|
name: &'static str,
|
2019-10-09 11:19:15 +00:00
|
|
|
mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
) -> PResult<'a, T> {
|
2024-03-04 05:31:49 +00:00
|
|
|
let mut parser = Parser::new(psess, tts, Some(name));
|
2019-10-09 11:19:15 +00:00
|
|
|
let result = f(&mut parser)?;
|
|
|
|
if parser.token != token::Eof {
|
|
|
|
parser.unexpected()?;
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2024-03-04 05:31:49 +00:00
|
|
|
pub fn fake_token_stream_for_item(psess: &ParseSess, item: &ast::Item) -> TokenStream {
|
2022-05-21 12:50:39 +00:00
|
|
|
let source = pprust::item_to_string(item);
|
2020-11-23 06:43:55 +00:00
|
|
|
let filename = FileName::macro_expansion_source_code(&source);
|
2024-05-31 05:43:18 +00:00
|
|
|
unwrap_or_emit_fatal(source_str_to_stream(psess, filename, source, Some(item.span)))
|
2020-08-01 11:59:02 +00:00
|
|
|
}
|
2021-07-29 17:00:41 +00:00
|
|
|
|
2024-03-04 05:31:49 +00:00
|
|
|
pub fn fake_token_stream_for_crate(psess: &ParseSess, krate: &ast::Crate) -> TokenStream {
|
2021-10-17 16:32:34 +00:00
|
|
|
let source = pprust::crate_to_string_for_macros(krate);
|
|
|
|
let filename = FileName::macro_expansion_source_code(&source);
|
2024-05-31 05:43:18 +00:00
|
|
|
unwrap_or_emit_fatal(source_str_to_stream(
|
|
|
|
psess,
|
|
|
|
filename,
|
|
|
|
source,
|
|
|
|
Some(krate.spans.inner_span),
|
|
|
|
))
|
2021-10-17 16:32:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 17:00:41 +00:00
|
|
|
pub fn parse_cfg_attr(
|
2024-07-10 05:11:57 +00:00
|
|
|
cfg_attr: &Attribute,
|
2024-03-04 05:31:49 +00:00
|
|
|
psess: &ParseSess,
|
2021-07-29 17:00:41 +00:00
|
|
|
) -> Option<(MetaItem, Vec<(AttrItem, Span)>)> {
|
2024-05-31 00:59:16 +00:00
|
|
|
const CFG_ATTR_GRAMMAR_HELP: &str = "#[cfg_attr(condition, attribute, other_attribute, ...)]";
|
|
|
|
const CFG_ATTR_NOTE_REF: &str = "for more information, visit \
|
|
|
|
<https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute>";
|
|
|
|
|
2024-07-10 05:11:57 +00:00
|
|
|
match cfg_attr.get_normal_item().args {
|
2022-11-18 00:24:21 +00:00
|
|
|
ast::AttrArgs::Delimited(ast::DelimArgs { dspan, delim, ref tokens })
|
|
|
|
if !tokens.is_empty() =>
|
|
|
|
{
|
2024-03-04 05:31:49 +00:00
|
|
|
crate::validate_attr::check_cfg_attr_bad_delim(psess, dspan, delim);
|
|
|
|
match parse_in(psess, tokens.clone(), "`cfg_attr` input", |p| p.parse_cfg_attr()) {
|
2021-07-29 17:00:41 +00:00
|
|
|
Ok(r) => return Some(r),
|
Make `DiagnosticBuilder::emit` consuming.
This works for most of its call sites. This is nice, because `emit` very
much makes sense as a consuming operation -- indeed,
`DiagnosticBuilderState` exists to ensure no diagnostic is emitted
twice, but it uses runtime checks.
For the small number of call sites where a consuming emit doesn't work,
the commit adds `DiagnosticBuilder::emit_without_consuming`. (This will
be removed in subsequent commits.)
Likewise, `emit_unless` becomes consuming. And `delay_as_bug` becomes
consuming, while `delay_as_bug_without_consuming` is added (which will
also be removed in subsequent commits.)
All this requires significant changes to `DiagnosticBuilder`'s chaining
methods. Currently `DiagnosticBuilder` method chaining uses a
non-consuming `&mut self -> &mut Self` style, which allows chaining to
be used when the chain ends in `emit()`, like so:
```
struct_err(msg).span(span).emit();
```
But it doesn't work when producing a `DiagnosticBuilder` value,
requiring this:
```
let mut err = self.struct_err(msg);
err.span(span);
err
```
This style of chaining won't work with consuming `emit` though. For
that, we need to use to a `self -> Self` style. That also would allow
`DiagnosticBuilder` production to be chained, e.g.:
```
self.struct_err(msg).span(span)
```
However, removing the `&mut self -> &mut Self` style would require that
individual modifications of a `DiagnosticBuilder` go from this:
```
err.span(span);
```
to this:
```
err = err.span(span);
```
There are *many* such places. I have a high tolerance for tedious
refactorings, but even I gave up after a long time trying to convert
them all.
Instead, this commit has it both ways: the existing `&mut self -> Self`
chaining methods are kept, and new `self -> Self` chaining methods are
added, all of which have a `_mv` suffix (short for "move"). Changes to
the existing `forward!` macro lets this happen with very little
additional boilerplate code. I chose to add the suffix to the new
chaining methods rather than the existing ones, because the number of
changes required is much smaller that way.
This doubled chainging is a bit clumsy, but I think it is worthwhile
because it allows a *lot* of good things to subsequently happen. In this
commit, there are many `mut` qualifiers removed in places where
diagnostics are emitted without being modified. In subsequent commits:
- chaining can be used more, making the code more concise;
- more use of chaining also permits the removal of redundant diagnostic
APIs like `struct_err_with_code`, which can be replaced easily with
`struct_err` + `code_mv`;
- `emit_without_diagnostic` can be removed, which simplifies a lot of
machinery, removing the need for `DiagnosticBuilderState`.
2024-01-03 01:17:35 +00:00
|
|
|
Err(e) => {
|
2024-01-08 22:08:49 +00:00
|
|
|
e.with_help(format!("the valid syntax is `{CFG_ATTR_GRAMMAR_HELP}`"))
|
|
|
|
.with_note(CFG_ATTR_NOTE_REF)
|
2021-07-29 17:00:41 +00:00
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-31 00:59:16 +00:00
|
|
|
_ => {
|
2024-06-18 09:43:28 +00:00
|
|
|
psess.dcx().emit_err(errors::MalformedCfgAttr {
|
2024-07-10 05:11:57 +00:00
|
|
|
span: cfg_attr.span,
|
2024-05-31 00:59:16 +00:00
|
|
|
sugg: CFG_ATTR_GRAMMAR_HELP,
|
|
|
|
});
|
|
|
|
}
|
2021-07-29 17:00:41 +00:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|