2024-04-26 08:44:23 +10:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::rc::Rc;
|
2025-02-03 06:44:41 +03:00
|
|
|
use std::sync::Arc;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast as ast;
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::ptr::P;
|
|
|
|
use rustc_ast::token;
|
|
|
|
use rustc_ast::tokenstream::TokenStream;
|
2020-01-11 17:02:46 +01:00
|
|
|
use rustc_ast_pretty::pprust;
|
2024-03-01 02:49:02 +00:00
|
|
|
use rustc_expand::base::{
|
2024-04-26 08:44:23 +10:00
|
|
|
DummyResult, ExpandResult, ExtCtxt, MacEager, MacResult, MacroExpanderResult, resolve_path,
|
2024-03-01 02:49:02 +00:00
|
|
|
};
|
2021-02-22 19:06:36 +03:00
|
|
|
use rustc_expand::module::DirOwnership;
|
2024-04-14 20:11:14 +00:00
|
|
|
use rustc_lint_defs::BuiltinLintDiag;
|
2021-01-18 16:47:37 -05:00
|
|
|
use rustc_parse::parser::{ForceCollect, Parser};
|
2025-01-15 21:24:31 +00:00
|
|
|
use rustc_parse::{new_parser_from_file, unwrap_or_emit_fatal, utf8_error};
|
2020-01-05 10:47:20 +01:00
|
|
|
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
|
2024-03-01 02:49:02 +00:00
|
|
|
use rustc_span::source_map::SourceMap;
|
2024-12-13 10:29:23 +11:00
|
|
|
use rustc_span::{Pos, Span, Symbol};
|
2018-08-30 11:42:16 +02:00
|
|
|
use smallvec::SmallVec;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2024-03-01 02:49:02 +00:00
|
|
|
use crate::errors;
|
|
|
|
use crate::util::{
|
|
|
|
check_zero_tts, get_single_str_from_tts, get_single_str_spanned_from_tts, parse_expr,
|
2020-03-20 16:02:46 +01:00
|
|
|
};
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2013-02-26 10:15:29 -08:00
|
|
|
// These macros all relate to the file system; they either return
|
|
|
|
// the column/row/filename of the expression, or they include
|
|
|
|
// a given file into the current one.
|
2013-02-11 18:13:18 +02:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// line!(): expands to the current line number
|
2024-04-26 07:56:48 +10:00
|
|
|
pub(crate) fn expand_line(
|
2019-08-31 20:08:06 +03:00
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2024-03-12 10:55:17 +08:00
|
|
|
) -> MacroExpanderResult<'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2024-02-25 22:25:26 +01:00
|
|
|
check_zero_tts(cx, sp, tts, "line!");
|
2013-02-11 18:13:18 +02:00
|
|
|
|
2017-05-15 09:41:05 +00:00
|
|
|
let topmost = cx.expansion_cause().unwrap_or(sp);
|
2018-08-18 12:14:09 +02:00
|
|
|
let loc = cx.source_map().lookup_char_pos(topmost.lo());
|
2013-02-11 18:13:18 +02:00
|
|
|
|
2024-03-12 10:55:17 +08:00
|
|
|
ExpandResult::Ready(MacEager::expr(cx.expr_u32(topmost, loc.line as u32)))
|
2012-05-14 17:43:31 -07:00
|
|
|
}
|
|
|
|
|
2014-11-18 23:03:58 +11:00
|
|
|
/* column!(): expands to the current column number */
|
2024-04-26 07:56:48 +10:00
|
|
|
pub(crate) fn expand_column(
|
2019-08-31 20:08:06 +03:00
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2024-03-12 10:55:17 +08:00
|
|
|
) -> MacroExpanderResult<'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2024-02-25 22:25:26 +01:00
|
|
|
check_zero_tts(cx, sp, tts, "column!");
|
2013-02-11 18:13:18 +02:00
|
|
|
|
2017-05-15 09:41:05 +00:00
|
|
|
let topmost = cx.expansion_cause().unwrap_or(sp);
|
2018-08-18 12:14:09 +02:00
|
|
|
let loc = cx.source_map().lookup_char_pos(topmost.lo());
|
2015-02-21 06:56:46 -05:00
|
|
|
|
2024-03-12 10:55:17 +08:00
|
|
|
ExpandResult::Ready(MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32 + 1)))
|
2012-05-14 17:43:31 -07:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// file!(): expands to the current filename */
|
2018-08-18 12:13:56 +02:00
|
|
|
/// The source_file (`loc.file`) contains a bunch more information we could spit
|
2014-06-09 13:12:30 -07:00
|
|
|
/// out if we wanted.
|
2024-04-26 07:56:48 +10:00
|
|
|
pub(crate) fn expand_file(
|
2019-08-31 20:08:06 +03:00
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2024-03-12 10:55:17 +08:00
|
|
|
) -> MacroExpanderResult<'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2024-02-25 22:25:26 +01:00
|
|
|
check_zero_tts(cx, sp, tts, "file!");
|
2013-02-11 18:13:18 +02:00
|
|
|
|
2017-05-15 09:41:05 +00:00
|
|
|
let topmost = cx.expansion_cause().unwrap_or(sp);
|
2018-08-18 12:14:09 +02:00
|
|
|
let loc = cx.source_map().lookup_char_pos(topmost.lo());
|
2023-08-23 15:46:58 +02:00
|
|
|
|
|
|
|
use rustc_session::RemapFileNameExt;
|
|
|
|
use rustc_session::config::RemapPathScopeComponents;
|
2024-03-12 10:55:17 +08:00
|
|
|
ExpandResult::Ready(MacEager::expr(cx.expr_str(
|
2023-08-23 15:46:58 +02:00
|
|
|
topmost,
|
|
|
|
Symbol::intern(
|
|
|
|
&loc.file.name.for_scope(cx.sess, RemapPathScopeComponents::MACRO).to_string_lossy(),
|
|
|
|
),
|
2024-03-12 10:55:17 +08:00
|
|
|
)))
|
2012-05-14 17:43:31 -07:00
|
|
|
}
|
|
|
|
|
2024-04-26 07:56:48 +10:00
|
|
|
pub(crate) fn expand_stringify(
|
2019-08-31 20:08:06 +03:00
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2024-03-12 10:55:17 +08:00
|
|
|
) -> MacroExpanderResult<'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2020-06-24 17:45:08 +03:00
|
|
|
let s = pprust::tts_to_string(&tts);
|
2024-03-12 10:55:17 +08:00
|
|
|
ExpandResult::Ready(MacEager::expr(cx.expr_str(sp, Symbol::intern(&s))))
|
2012-05-14 17:43:31 -07:00
|
|
|
}
|
|
|
|
|
2024-04-26 07:56:48 +10:00
|
|
|
pub(crate) fn expand_mod(
|
2019-08-31 20:08:06 +03:00
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2024-03-12 10:55:17 +08:00
|
|
|
) -> MacroExpanderResult<'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2024-02-25 22:25:26 +01:00
|
|
|
check_zero_tts(cx, sp, tts, "module_path!");
|
2016-09-07 23:21:59 +00:00
|
|
|
let mod_path = &cx.current_expansion.module.mod_path;
|
|
|
|
let string = mod_path.iter().map(|x| x.to_string()).collect::<Vec<String>>().join("::");
|
2016-09-01 06:44:54 +00:00
|
|
|
|
2024-03-12 10:55:17 +08:00
|
|
|
ExpandResult::Ready(MacEager::expr(cx.expr_str(sp, Symbol::intern(&string))))
|
2012-05-18 10:02:21 -07:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// include! : parse the given file as an expr
|
|
|
|
/// This is generally a bad idea because it's going to behave
|
|
|
|
/// unhygienically.
|
2024-04-26 07:56:48 +10:00
|
|
|
pub(crate) fn expand_include<'cx>(
|
2019-08-31 20:08:06 +03:00
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2024-03-12 10:55:17 +08:00
|
|
|
) -> MacroExpanderResult<'cx> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2024-03-12 10:55:17 +08:00
|
|
|
let ExpandResult::Ready(mac) = get_single_str_from_tts(cx, sp, tts, "include!") else {
|
|
|
|
return ExpandResult::Retry(());
|
|
|
|
};
|
|
|
|
let file = match mac {
|
2024-02-25 22:22:11 +01:00
|
|
|
Ok(file) => file,
|
2024-03-12 10:55:17 +08:00
|
|
|
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2013-11-27 20:05:12 -07:00
|
|
|
// The file will be added to the code map by the parser
|
2024-01-10 00:37:30 -05:00
|
|
|
let file = match resolve_path(&cx.sess, file.as_str(), sp) {
|
2019-10-19 13:05:46 -04:00
|
|
|
Ok(f) => f,
|
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 12:17:35 +11:00
|
|
|
Err(err) => {
|
2024-02-25 22:22:11 +01:00
|
|
|
let guar = err.emit();
|
2024-03-12 10:55:17 +08:00
|
|
|
return ExpandResult::Ready(DummyResult::any(sp, guar));
|
2019-10-19 13:05:46 -04:00
|
|
|
}
|
|
|
|
};
|
2024-05-31 15:43:18 +10:00
|
|
|
let p = unwrap_or_emit_fatal(new_parser_from_file(cx.psess(), &file, Some(sp)));
|
2014-10-20 23:04:16 -07:00
|
|
|
|
2020-03-20 16:02:46 +01:00
|
|
|
// If in the included file we have e.g., `mod bar;`,
|
|
|
|
// then the path of `bar.rs` should be relative to the directory of `file`.
|
|
|
|
// See https://github.com/rust-lang/rust/pull/69838/files#r395217057 for a discussion.
|
|
|
|
// `MacroExpander::fully_expand_fragment` later restores, so "stack discipline" is maintained.
|
2021-02-21 19:15:43 +03:00
|
|
|
let dir_path = file.parent().unwrap_or(&file).to_owned();
|
|
|
|
cx.current_expansion.module = Rc::new(cx.current_expansion.module.with_dir_path(dir_path));
|
2021-02-22 19:06:36 +03:00
|
|
|
cx.current_expansion.dir_ownership = DirOwnership::Owned { relative: None };
|
2020-03-20 16:02:46 +01:00
|
|
|
|
2024-03-12 10:55:17 +08:00
|
|
|
struct ExpandInclude<'a> {
|
2019-10-15 22:48:13 +02:00
|
|
|
p: Parser<'a>,
|
2020-06-09 20:59:43 +03:00
|
|
|
node_id: ast::NodeId,
|
2014-10-20 23:04:16 -07:00
|
|
|
}
|
2024-03-12 10:55:17 +08:00
|
|
|
impl<'a> MacResult for ExpandInclude<'a> {
|
|
|
|
fn make_expr(mut self: Box<ExpandInclude<'a>>) -> Option<P<ast::Expr>> {
|
2024-02-25 22:25:26 +01:00
|
|
|
let expr = parse_expr(&mut self.p).ok()?;
|
2019-09-08 10:00:26 -04:00
|
|
|
if self.p.token != token::Eof {
|
2024-05-20 17:47:54 +00:00
|
|
|
self.p.psess.buffer_lint(
|
2023-11-21 20:07:32 +01:00
|
|
|
INCOMPLETE_INCLUDE,
|
2019-09-08 10:00:26 -04:00
|
|
|
self.p.token.span,
|
2020-06-09 20:59:43 +03:00
|
|
|
self.node_id,
|
2024-04-14 20:11:14 +00:00
|
|
|
BuiltinLintDiag::IncompleteInclude,
|
2019-09-08 10:00:26 -04:00
|
|
|
);
|
|
|
|
}
|
2024-02-25 22:22:11 +01:00
|
|
|
Some(expr)
|
2014-10-20 23:04:16 -07:00
|
|
|
}
|
2018-08-30 11:42:16 +02:00
|
|
|
|
2024-03-12 10:55:17 +08:00
|
|
|
fn make_items(mut self: Box<ExpandInclude<'a>>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
|
2018-08-30 11:42:16 +02:00
|
|
|
let mut ret = SmallVec::new();
|
2022-03-22 00:36:47 +01:00
|
|
|
loop {
|
2021-01-18 16:47:37 -05:00
|
|
|
match self.p.parse_item(ForceCollect::No) {
|
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 12:17:35 +11:00
|
|
|
Err(err) => {
|
2020-03-17 08:59:56 +01:00
|
|
|
err.emit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(Some(item)) => ret.push(item),
|
|
|
|
Ok(None) => {
|
2022-03-22 00:36:47 +01:00
|
|
|
if self.p.token != token::Eof {
|
2024-06-25 18:04:21 +08:00
|
|
|
self.p
|
|
|
|
.dcx()
|
|
|
|
.create_err(errors::ExpectedItem {
|
|
|
|
span: self.p.token.span,
|
|
|
|
token: &pprust::token_to_string(&self.p.token),
|
|
|
|
})
|
|
|
|
.emit();
|
2022-03-22 00:36:47 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 08:59:56 +01:00
|
|
|
break;
|
2019-12-07 03:07:35 +01:00
|
|
|
}
|
2014-10-20 23:04:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 10:55:17 +08:00
|
|
|
ExpandResult::Ready(Box::new(ExpandInclude { p, node_id: cx.current_expansion.lint_node_id }))
|
2012-05-14 17:43:31 -07:00
|
|
|
}
|
2012-05-18 10:00:49 -07:00
|
|
|
|
2022-11-27 11:15:06 +00:00
|
|
|
/// `include_str!`: read the given file, insert it as a literal string expr
|
2024-04-26 07:56:48 +10:00
|
|
|
pub(crate) fn expand_include_str(
|
2019-08-31 20:08:06 +03:00
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2024-03-12 10:55:17 +08:00
|
|
|
) -> MacroExpanderResult<'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2024-03-01 02:49:02 +00:00
|
|
|
let ExpandResult::Ready(mac) = get_single_str_spanned_from_tts(cx, sp, tts, "include_str!")
|
|
|
|
else {
|
2024-03-12 10:55:17 +08:00
|
|
|
return ExpandResult::Retry(());
|
|
|
|
};
|
2024-03-01 02:49:02 +00:00
|
|
|
let (path, path_span) = match mac {
|
|
|
|
Ok(res) => res,
|
2024-03-12 10:55:17 +08:00
|
|
|
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2024-03-01 02:49:02 +00:00
|
|
|
ExpandResult::Ready(match load_binary_file(cx, path.as_str().as_ref(), sp, path_span) {
|
2024-03-29 12:31:34 -07:00
|
|
|
Ok((bytes, bsp)) => match std::str::from_utf8(&bytes) {
|
2019-08-13 19:51:32 +03:00
|
|
|
Ok(src) => {
|
2023-11-21 20:07:32 +01:00
|
|
|
let interned_src = Symbol::intern(src);
|
2024-03-29 12:31:34 -07:00
|
|
|
MacEager::expr(cx.expr_str(cx.with_def_site_ctxt(bsp), interned_src))
|
2019-08-13 19:51:32 +03:00
|
|
|
}
|
2025-01-15 21:24:31 +00:00
|
|
|
Err(utf8err) => {
|
|
|
|
let mut err = cx.dcx().struct_span_err(sp, format!("`{path}` wasn't a utf-8 file"));
|
|
|
|
utf8_error(cx.source_map(), path.as_str(), None, &mut err, utf8err, &bytes[..]);
|
|
|
|
DummyResult::any(sp, err.emit())
|
2019-08-13 19:51:32 +03:00
|
|
|
}
|
2018-11-16 16:22:06 -05:00
|
|
|
},
|
2024-03-01 02:49:02 +00:00
|
|
|
Err(dummy) => dummy,
|
2024-03-12 10:55:17 +08:00
|
|
|
})
|
2012-05-18 10:02:21 -07:00
|
|
|
}
|
|
|
|
|
2024-04-26 07:56:48 +10:00
|
|
|
pub(crate) fn expand_include_bytes(
|
2019-08-31 20:08:06 +03:00
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2024-03-12 10:55:17 +08:00
|
|
|
) -> MacroExpanderResult<'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2024-03-01 02:49:02 +00:00
|
|
|
let ExpandResult::Ready(mac) = get_single_str_spanned_from_tts(cx, sp, tts, "include_bytes!")
|
|
|
|
else {
|
2024-03-12 10:55:17 +08:00
|
|
|
return ExpandResult::Retry(());
|
|
|
|
};
|
2024-03-01 02:49:02 +00:00
|
|
|
let (path, path_span) = match mac {
|
|
|
|
Ok(res) => res,
|
2024-03-12 10:55:17 +08:00
|
|
|
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2024-03-01 02:49:02 +00:00
|
|
|
ExpandResult::Ready(match load_binary_file(cx, path.as_str().as_ref(), sp, path_span) {
|
2024-03-29 12:31:34 -07:00
|
|
|
Ok((bytes, _bsp)) => {
|
|
|
|
// Don't care about getting the span for the raw bytes,
|
|
|
|
// because the console can't really show them anyway.
|
2024-03-01 02:49:02 +00:00
|
|
|
let expr = cx.expr(sp, ast::ExprKind::IncludedBytes(bytes));
|
|
|
|
MacEager::expr(expr)
|
|
|
|
}
|
|
|
|
Err(dummy) => dummy,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_binary_file(
|
compiler: fix few needless_pass_by_ref_mut clippy lints
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\asm.rs:306:28
|
306 | fn err_duplicate_option(p: &mut Parser<'_>, symbol: Symbol, span: Span) {
| ^^^^^^^^^^^^^^^ help: consider changing to: `&Parser<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\asm.rs:318:8
|
318 | p: &mut Parser<'a>,
| ^^^^^^^^^^^^^^^ help: consider changing to: `&Parser<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\assert.rs:114:25
|
114 | fn parse_assert<'a>(cx: &mut ExtCtxt<'a>, sp: Span, stream: TokenStream) -> PResult<'a, Assert> {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\asm.rs:32:10
|
32 | ecx: &mut ExtCtxt<'a>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\test.rs:99:9
|
99 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\source_util.rs:237:9
|
237 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\format.rs:809:10
|
809 | ecx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\format.rs:737:10
|
737 | ecx: &mut ExtCtxt<'a>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\format.rs:68:24
|
68 | fn parse_args<'a>(ecx: &mut ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a, MacroInput> {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\format.rs:607:10
|
607 | ecx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\edition_panic.rs:43:9
|
43 | cx: &'cx mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\concat_bytes.rs:11:9
|
11 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\cfg.rs:38:22
|
38 | fn parse_cfg<'a>(cx: &mut ExtCtxt<'a>, span: Span, tts: TokenStream) -> PResult<'a, ast::MetaItem> {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\cfg_accessible.rs:13:28
|
13 | fn validate_input<'a>(ecx: &mut ExtCtxt<'_>, mi: &'a ast::MetaItem) -> Option<&'a ast::Path> {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
2024-03-28 12:04:00 +03:00
|
|
|
cx: &ExtCtxt<'_>,
|
2024-03-01 02:49:02 +00:00
|
|
|
original_path: &Path,
|
|
|
|
macro_span: Span,
|
|
|
|
path_span: Span,
|
2025-02-03 06:44:41 +03:00
|
|
|
) -> Result<(Arc<[u8]>, Span), Box<dyn MacResult>> {
|
2024-03-01 02:49:02 +00:00
|
|
|
let resolved_path = match resolve_path(&cx.sess, original_path, macro_span) {
|
|
|
|
Ok(path) => path,
|
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 12:17:35 +11:00
|
|
|
Err(err) => {
|
2024-02-25 22:22:11 +01:00
|
|
|
let guar = err.emit();
|
2024-03-01 02:49:02 +00:00
|
|
|
return Err(DummyResult::any(macro_span, guar));
|
2019-10-19 13:05:46 -04:00
|
|
|
}
|
|
|
|
};
|
2024-03-01 02:49:02 +00:00
|
|
|
match cx.source_map().load_binary_file(&resolved_path) {
|
|
|
|
Ok(data) => Ok(data),
|
|
|
|
Err(io_err) => {
|
|
|
|
let mut err = cx.dcx().struct_span_err(
|
|
|
|
macro_span,
|
|
|
|
format!("couldn't read `{}`: {io_err}", resolved_path.display()),
|
|
|
|
);
|
|
|
|
|
|
|
|
if original_path.is_relative() {
|
|
|
|
let source_map = cx.sess.source_map();
|
|
|
|
let new_path = source_map
|
|
|
|
.span_to_filename(macro_span.source_callsite())
|
|
|
|
.into_local_path()
|
|
|
|
.and_then(|src| find_path_suggestion(source_map, src.parent()?, original_path))
|
|
|
|
.and_then(|path| path.into_os_string().into_string().ok());
|
|
|
|
|
|
|
|
if let Some(new_path) = new_path {
|
2025-01-15 21:24:31 +00:00
|
|
|
err.span_suggestion_verbose(
|
2024-03-01 02:49:02 +00:00
|
|
|
path_span,
|
|
|
|
"there is a file with the same name in a different directory",
|
|
|
|
format!("\"{}\"", new_path.replace('\\', "/").escape_debug()),
|
|
|
|
rustc_lint_defs::Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let guar = err.emit();
|
|
|
|
Err(DummyResult::any(macro_span, guar))
|
2022-10-31 18:30:09 +00:00
|
|
|
}
|
2024-03-01 02:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_path_suggestion(
|
|
|
|
source_map: &SourceMap,
|
|
|
|
base_dir: &Path,
|
|
|
|
wanted_path: &Path,
|
|
|
|
) -> Option<PathBuf> {
|
|
|
|
// Fix paths that assume they're relative to cargo manifest dir
|
|
|
|
let mut base_c = base_dir.components();
|
|
|
|
let mut wanted_c = wanted_path.components();
|
|
|
|
let mut without_base = None;
|
|
|
|
while let Some(wanted_next) = wanted_c.next() {
|
|
|
|
if wanted_c.as_path().file_name().is_none() {
|
|
|
|
break;
|
2013-10-14 08:24:17 -07:00
|
|
|
}
|
2024-03-01 02:49:02 +00:00
|
|
|
// base_dir may be absolute
|
|
|
|
while let Some(base_next) = base_c.next() {
|
|
|
|
if base_next == wanted_next {
|
|
|
|
without_base = Some(wanted_c.as_path());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let root_absolute = without_base.into_iter().map(PathBuf::from);
|
|
|
|
|
|
|
|
let base_dir_components = base_dir.components().count();
|
|
|
|
// Avoid going all the way to the root dir
|
|
|
|
let max_parent_components = if base_dir.is_relative() {
|
|
|
|
base_dir_components + 1
|
|
|
|
} else {
|
|
|
|
base_dir_components.saturating_sub(1)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Try with additional leading ../
|
|
|
|
let mut prefix = PathBuf::new();
|
|
|
|
let add = std::iter::from_fn(|| {
|
|
|
|
prefix.push("..");
|
|
|
|
Some(prefix.join(wanted_path))
|
2024-03-12 10:55:17 +08:00
|
|
|
})
|
2024-03-01 02:49:02 +00:00
|
|
|
.take(max_parent_components.min(3));
|
|
|
|
|
|
|
|
// Try without leading directories
|
|
|
|
let mut trimmed_path = wanted_path;
|
|
|
|
let remove = std::iter::from_fn(|| {
|
|
|
|
let mut components = trimmed_path.components();
|
|
|
|
let removed = components.next()?;
|
|
|
|
trimmed_path = components.as_path();
|
|
|
|
let _ = trimmed_path.file_name()?; // ensure there is a file name left
|
|
|
|
Some([
|
|
|
|
Some(trimmed_path.to_path_buf()),
|
|
|
|
(removed != std::path::Component::ParentDir)
|
|
|
|
.then(|| Path::new("..").join(trimmed_path)),
|
|
|
|
])
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.flatten()
|
|
|
|
.take(4);
|
|
|
|
|
2024-04-07 17:59:11 +02:00
|
|
|
root_absolute
|
|
|
|
.chain(add)
|
|
|
|
.chain(remove)
|
|
|
|
.find(|new_path| source_map.file_exists(&base_dir.join(&new_path)))
|
2012-05-18 10:03:27 -07:00
|
|
|
}
|