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;
|
2019-12-29 17:23:55 +03:00
|
|
|
use rustc_expand::base::{self, *};
|
2021-02-22 19:06:36 +03:00
|
|
|
use rustc_expand::module::DirOwnership;
|
2023-11-10 10:11:24 +08:00
|
|
|
use rustc_parse::new_parser_from_file;
|
2021-01-18 16:47:37 -05:00
|
|
|
use rustc_parse::parser::{ForceCollect, Parser};
|
2020-01-05 10:47:20 +01:00
|
|
|
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::Symbol;
|
2023-11-10 10:11:24 +08:00
|
|
|
use rustc_span::{Pos, Span};
|
2019-02-07 02:33:01 +09:00
|
|
|
|
2018-08-30 11:42:16 +02:00
|
|
|
use smallvec::SmallVec;
|
2020-03-20 16:02:46 +01:00
|
|
|
use std::rc::Rc;
|
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
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_line(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2012-12-12 17:08:09 -08:00
|
|
|
base::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
|
|
|
|
2015-02-27 11:14:42 -08:00
|
|
|
base::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 */
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_column(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2014-11-18 23:03:58 +11:00
|
|
|
base::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
|
|
|
|
2017-12-24 02:20:06 +01:00
|
|
|
base::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.
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_file(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2012-12-12 17:08:09 -08:00
|
|
|
base::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::{config::RemapPathScopeComponents, RemapFileNameExt};
|
|
|
|
base::MacEager::expr(cx.expr_str(
|
|
|
|
topmost,
|
|
|
|
Symbol::intern(
|
|
|
|
&loc.file.name.for_scope(cx.sess, RemapPathScopeComponents::MACRO).to_string_lossy(),
|
|
|
|
),
|
|
|
|
))
|
2012-05-14 17:43:31 -07:00
|
|
|
}
|
|
|
|
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_stringify(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + '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);
|
2016-11-16 10:52:37 +00:00
|
|
|
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&s)))
|
2012-05-14 17:43:31 -07:00
|
|
|
}
|
|
|
|
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_mod(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2012-12-12 17:08:09 -08:00
|
|
|
base::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
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
base::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.
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_include<'cx>(
|
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'cx> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2024-02-25 22:22:11 +01:00
|
|
|
let file = match get_single_str_from_tts(cx, sp, tts, "include!") {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(guar) => return 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();
|
|
|
|
return DummyResult::any(sp, guar);
|
2019-10-19 13:05:46 -04:00
|
|
|
}
|
|
|
|
};
|
2020-03-21 23:10:10 +01:00
|
|
|
let p = new_parser_from_file(cx.parse_sess(), &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
|
|
|
|
2014-10-20 23:04:16 -07:00
|
|
|
struct ExpandResult<'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
|
|
|
}
|
|
|
|
impl<'a> base::MacResult for ExpandResult<'a> {
|
|
|
|
fn make_expr(mut self: Box<ExpandResult<'a>>) -> Option<P<ast::Expr>> {
|
2024-02-25 22:22:11 +01:00
|
|
|
let expr = base::parse_expr(&mut self.p).ok()?;
|
2019-09-08 10:00:26 -04:00
|
|
|
if self.p.token != token::Eof {
|
|
|
|
self.p.sess.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,
|
2019-09-08 10:00:26 -04:00
|
|
|
"include macro expected single expression in source",
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
fn make_items(mut self: Box<ExpandResult<'a>>) -> Option<SmallVec<[P<ast::Item>; 1]>> {
|
|
|
|
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 {
|
|
|
|
let token = pprust::token_to_string(&self.p.token);
|
2023-07-25 22:00:13 +02:00
|
|
|
let msg = format!("expected item, found `{token}`");
|
2024-01-04 10:38:10 +11:00
|
|
|
self.p.dcx().span_err(self.p.token.span, msg);
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 18:24:12 -05:00
|
|
|
Box::new(ExpandResult { 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
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_include_str(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2024-02-25 22:22:11 +01:00
|
|
|
let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(guar) => return DummyResult::any(sp, guar),
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
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();
|
|
|
|
return DummyResult::any(sp, guar);
|
2019-10-19 13:05:46 -04:00
|
|
|
}
|
|
|
|
};
|
2019-08-13 19:51:32 +03:00
|
|
|
match cx.source_map().load_binary_file(&file) {
|
|
|
|
Ok(bytes) => match std::str::from_utf8(&bytes) {
|
|
|
|
Ok(src) => {
|
2023-11-21 20:07:32 +01:00
|
|
|
let interned_src = Symbol::intern(src);
|
2019-08-13 19:51:32 +03:00
|
|
|
base::MacEager::expr(cx.expr_str(sp, interned_src))
|
|
|
|
}
|
|
|
|
Err(_) => {
|
2024-02-25 22:22:11 +01:00
|
|
|
let guar = cx.dcx().span_err(sp, format!("{} wasn't a utf-8 file", file.display()));
|
|
|
|
DummyResult::any(sp, guar)
|
2019-08-13 19:51:32 +03:00
|
|
|
}
|
2018-11-16 16:22:06 -05:00
|
|
|
},
|
|
|
|
Err(e) => {
|
2024-02-25 22:22:11 +01:00
|
|
|
let guar = cx.dcx().span_err(sp, format!("couldn't read {}: {}", file.display(), e));
|
|
|
|
DummyResult::any(sp, guar)
|
2013-10-13 18:48:47 -07:00
|
|
|
}
|
2012-05-18 10:02:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 20:08:06 +03:00
|
|
|
pub fn expand_include_bytes(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2018-07-10 21:06:26 +02:00
|
|
|
) -> Box<dyn base::MacResult + 'static> {
|
2019-11-12 17:48:33 -05:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2024-02-25 22:22:11 +01:00
|
|
|
let file = match get_single_str_from_tts(cx, sp, tts, "include_bytes!") {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(guar) => return DummyResult::any(sp, guar),
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
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();
|
|
|
|
return DummyResult::any(sp, guar);
|
2019-10-19 13:05:46 -04:00
|
|
|
}
|
|
|
|
};
|
2019-08-13 19:51:32 +03:00
|
|
|
match cx.source_map().load_binary_file(&file) {
|
2022-10-31 18:30:09 +00:00
|
|
|
Ok(bytes) => {
|
2023-08-27 18:16:12 -04:00
|
|
|
let expr = cx.expr(sp, ast::ExprKind::IncludedBytes(bytes));
|
2022-10-31 18:30:09 +00:00
|
|
|
base::MacEager::expr(expr)
|
|
|
|
}
|
2018-11-16 16:22:06 -05:00
|
|
|
Err(e) => {
|
2024-02-25 22:22:11 +01:00
|
|
|
let guar = cx.dcx().span_err(sp, format!("couldn't read {}: {}", file.display(), e));
|
|
|
|
DummyResult::any(sp, guar)
|
2013-10-14 08:24:17 -07:00
|
|
|
}
|
2012-05-18 10:03:27 -07:00
|
|
|
}
|
|
|
|
}
|