Skip to content

Commit 322e92b

Browse files
committed
Auto merge of #123856 - matthiaskrgr:rollup-4v8rkfj, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #123204 (rustdoc: point at span in `include_str!`-ed md file) - #123223 (Fix invalid silencing of parsing error) - #123249 (do not add prolog for variadic naked functions) - #123825 (Call the panic hook for non-unwind panics in proc-macros) - #123833 (Update stdarch submodule) - #123841 (Improve diagnostic by suggesting to remove visibility qualifier) - #123849 (Update E0384.md) - #123852 (fix typo in library/std/src/lib.rs) r? `@ghost` `@rustbot` modify labels: rollup
2 parents bd71213 + 4393eab commit 322e92b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+347
-118
lines changed

Diff for: compiler/rustc_ast_passes/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ ast_passes_visibility_not_permitted =
273273
.trait_impl = trait items always share the visibility of their trait
274274
.individual_impl_items = place qualifiers on individual impl items instead
275275
.individual_foreign_items = place qualifiers on individual foreign items instead
276+
.remove_qualifier_sugg = remove the qualifier
276277
277278
ast_passes_where_clause_after_type_alias = where clauses are not allowed after the type for type aliases
278279
.note = see issue #112792 <https://github.com/rust-lang/rust/issues/112792> for more information

Diff for: compiler/rustc_ast_passes/src/ast_validation.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,11 @@ impl<'a> AstValidator<'a> {
266266
return;
267267
}
268268

269-
self.dcx().emit_err(errors::VisibilityNotPermitted { span: vis.span, note });
269+
self.dcx().emit_err(errors::VisibilityNotPermitted {
270+
span: vis.span,
271+
note,
272+
remove_qualifier_sugg: vis.span,
273+
});
270274
}
271275

272276
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) {

Diff for: compiler/rustc_ast_passes/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ pub struct VisibilityNotPermitted {
3131
pub span: Span,
3232
#[subdiagnostic]
3333
pub note: VisibilityNotPermittedNote,
34+
#[suggestion(
35+
ast_passes_remove_qualifier_sugg,
36+
code = "",
37+
applicability = "machine-applicable"
38+
)]
39+
pub remove_qualifier_sugg: Span,
3440
}
3541

3642
#[derive(Subdiagnostic)]

Diff for: compiler/rustc_builtin_macros/src/source_util.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ pub fn expand_include_str(
196196
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
197197
};
198198
ExpandResult::Ready(match load_binary_file(cx, path.as_str().as_ref(), sp, path_span) {
199-
Ok(bytes) => match std::str::from_utf8(&bytes) {
199+
Ok((bytes, bsp)) => match std::str::from_utf8(&bytes) {
200200
Ok(src) => {
201201
let interned_src = Symbol::intern(src);
202-
MacEager::expr(cx.expr_str(sp, interned_src))
202+
MacEager::expr(cx.expr_str(cx.with_def_site_ctxt(bsp), interned_src))
203203
}
204204
Err(_) => {
205205
let guar = cx.dcx().span_err(sp, format!("`{path}` wasn't a utf-8 file"));
@@ -225,7 +225,9 @@ pub fn expand_include_bytes(
225225
Err(guar) => return ExpandResult::Ready(DummyResult::any(sp, guar)),
226226
};
227227
ExpandResult::Ready(match load_binary_file(cx, path.as_str().as_ref(), sp, path_span) {
228-
Ok(bytes) => {
228+
Ok((bytes, _bsp)) => {
229+
// Don't care about getting the span for the raw bytes,
230+
// because the console can't really show them anyway.
229231
let expr = cx.expr(sp, ast::ExprKind::IncludedBytes(bytes));
230232
MacEager::expr(expr)
231233
}
@@ -238,7 +240,7 @@ fn load_binary_file(
238240
original_path: &Path,
239241
macro_span: Span,
240242
path_span: Span,
241-
) -> Result<Lrc<[u8]>, Box<dyn MacResult>> {
243+
) -> Result<(Lrc<[u8]>, Span), Box<dyn MacResult>> {
242244
let resolved_path = match resolve_path(&cx.sess, original_path, macro_span) {
243245
Ok(path) => path,
244246
Err(err) => {

Diff for: compiler/rustc_codegen_ssa/src/mir/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::base;
22
use crate::traits::*;
33
use rustc_index::bit_set::BitSet;
44
use rustc_index::IndexVec;
5+
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
56
use rustc_middle::mir;
67
use rustc_middle::mir::traversal;
78
use rustc_middle::mir::UnwindTerminateReason;
@@ -289,6 +290,12 @@ fn arg_local_refs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
289290

290291
let mut num_untupled = None;
291292

293+
let codegen_fn_attrs = bx.tcx().codegen_fn_attrs(fx.instance.def_id());
294+
let naked = codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED);
295+
if naked {
296+
return vec![];
297+
}
298+
292299
let args = mir
293300
.args_iter()
294301
.enumerate()

Diff for: compiler/rustc_error_codes/src/error_codes/E0384.md

+13
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,16 @@ fn main() {
1818
x = 5;
1919
}
2020
```
21+
22+
Alternatively, you might consider initializing a new variable: either with a new
23+
bound name or (by [shadowing]) with the bound name of your existing variable.
24+
For example:
25+
26+
[shadowing]: https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing
27+
28+
```
29+
fn main() {
30+
let x = 3;
31+
let x = 5;
32+
}
33+
```

Diff for: compiler/rustc_errors/src/emitter.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,9 @@ impl HumanEmitter {
15131513
for line_idx in 0..annotated_file.lines.len() {
15141514
let file = annotated_file.file.clone();
15151515
let line = &annotated_file.lines[line_idx];
1516-
if let Some(source_string) = file.get_line(line.line_index - 1) {
1516+
if let Some(source_string) =
1517+
line.line_index.checked_sub(1).and_then(|l| file.get_line(l))
1518+
{
15171519
let leading_whitespace = source_string
15181520
.chars()
15191521
.take_while(|c| c.is_whitespace())
@@ -1553,7 +1555,10 @@ impl HumanEmitter {
15531555
for line in &annotated_file.lines {
15541556
max_line_len = max(
15551557
max_line_len,
1556-
annotated_file.file.get_line(line.line_index - 1).map_or(0, |s| s.len()),
1558+
line.line_index
1559+
.checked_sub(1)
1560+
.and_then(|l| annotated_file.file.get_line(l))
1561+
.map_or(0, |s| s.len()),
15571562
);
15581563
for ann in &line.annotations {
15591564
span_right_margin = max(span_right_margin, ann.start_col.display);

Diff for: compiler/rustc_parse/src/lexer/mod.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -697,33 +697,27 @@ impl<'psess, 'src> StringReader<'psess, 'src> {
697697
let expn_data = prefix_span.ctxt().outer_expn_data();
698698

699699
if expn_data.edition >= Edition::Edition2021 {
700-
let mut silence = false;
701700
// In Rust 2021, this is a hard error.
702701
let sugg = if prefix == "rb" {
703702
Some(errors::UnknownPrefixSugg::UseBr(prefix_span))
704703
} else if expn_data.is_root() {
705704
if self.cursor.first() == '\''
706705
&& let Some(start) = self.last_lifetime
707706
&& self.cursor.third() != '\''
707+
&& let end = self.mk_sp(self.pos, self.pos + BytePos(1))
708+
&& !self.psess.source_map().is_multiline(start.until(end))
708709
{
709-
// An "unclosed `char`" error will be emitted already, silence redundant error.
710-
silence = true;
711-
Some(errors::UnknownPrefixSugg::MeantStr {
712-
start,
713-
end: self.mk_sp(self.pos, self.pos + BytePos(1)),
714-
})
710+
// FIXME: An "unclosed `char`" error will be emitted already in some cases,
711+
// but it's hard to silence this error while not also silencing important cases
712+
// too. We should use the error stashing machinery instead.
713+
Some(errors::UnknownPrefixSugg::MeantStr { start, end })
715714
} else {
716715
Some(errors::UnknownPrefixSugg::Whitespace(prefix_span.shrink_to_hi()))
717716
}
718717
} else {
719718
None
720719
};
721-
let err = errors::UnknownPrefix { span: prefix_span, prefix, sugg };
722-
if silence {
723-
self.dcx().create_err(err).delay_as_bug();
724-
} else {
725-
self.dcx().emit_err(err);
726-
}
720+
self.dcx().emit_err(errors::UnknownPrefix { span: prefix_span, prefix, sugg });
727721
} else {
728722
// Before Rust 2021, only emit a lint for migration.
729723
self.psess.buffer_lint_with_diagnostic(

Diff for: compiler/rustc_resolve/src/rustdoc.rs

+38-7
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,12 @@ pub fn attrs_to_doc_fragments<'a>(
194194
for (attr, item_id) in attrs {
195195
if let Some((doc_str, comment_kind)) = attr.doc_str_and_comment_kind() {
196196
let doc = beautify_doc_string(doc_str, comment_kind);
197-
let kind = if attr.is_doc_comment() {
198-
DocFragmentKind::SugaredDoc
197+
let (span, kind) = if attr.is_doc_comment() {
198+
(attr.span, DocFragmentKind::SugaredDoc)
199199
} else {
200-
DocFragmentKind::RawDoc
200+
(span_for_value(attr), DocFragmentKind::RawDoc)
201201
};
202-
let fragment = DocFragment { span: attr.span, doc, kind, item_id, indent: 0 };
202+
let fragment = DocFragment { span, doc, kind, item_id, indent: 0 };
203203
doc_fragments.push(fragment);
204204
} else if !doc_only {
205205
other_attrs.push(attr.clone());
@@ -211,6 +211,16 @@ pub fn attrs_to_doc_fragments<'a>(
211211
(doc_fragments, other_attrs)
212212
}
213213

214+
fn span_for_value(attr: &ast::Attribute) -> Span {
215+
if let ast::AttrKind::Normal(normal) = &attr.kind
216+
&& let ast::AttrArgs::Eq(_, ast::AttrArgsEq::Hir(meta)) = &normal.item.args
217+
{
218+
meta.span.with_ctxt(attr.span.ctxt())
219+
} else {
220+
attr.span
221+
}
222+
}
223+
214224
/// Return the doc-comments on this item, grouped by the module they came from.
215225
/// The module can be different if this is a re-export with added documentation.
216226
///
@@ -482,15 +492,36 @@ pub fn span_of_fragments(fragments: &[DocFragment]) -> Option<Span> {
482492

483493
/// Attempts to match a range of bytes from parsed markdown to a `Span` in the source code.
484494
///
485-
/// This method will return `None` if we cannot construct a span from the source map or if the
486-
/// fragments are not all sugared doc comments. It's difficult to calculate the correct span in
487-
/// that case due to escaping and other source features.
495+
/// This method does not always work, because markdown bytes don't necessarily match source bytes,
496+
/// like if escapes are used in the string. In this case, it returns `None`.
497+
///
498+
/// This method will return `Some` only if:
499+
///
500+
/// - The doc is made entirely from sugared doc comments, which cannot contain escapes
501+
/// - The doc is entirely from a single doc fragment, with a string literal, exactly equal
502+
/// - The doc comes from `include_str!`
488503
pub fn source_span_for_markdown_range(
489504
tcx: TyCtxt<'_>,
490505
markdown: &str,
491506
md_range: &Range<usize>,
492507
fragments: &[DocFragment],
493508
) -> Option<Span> {
509+
if let &[fragment] = &fragments
510+
&& fragment.kind == DocFragmentKind::RawDoc
511+
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(fragment.span)
512+
&& snippet.trim_end() == markdown.trim_end()
513+
&& let Ok(md_range_lo) = u32::try_from(md_range.start)
514+
&& let Ok(md_range_hi) = u32::try_from(md_range.end)
515+
{
516+
// Single fragment with string that contains same bytes as doc.
517+
return Some(Span::new(
518+
fragment.span.lo() + rustc_span::BytePos(md_range_lo),
519+
fragment.span.lo() + rustc_span::BytePos(md_range_hi),
520+
fragment.span.ctxt(),
521+
fragment.span.parent(),
522+
));
523+
}
524+
494525
let is_all_sugared_doc = fragments.iter().all(|frag| frag.kind == DocFragmentKind::SugaredDoc);
495526

496527
if !is_all_sugared_doc {

Diff for: compiler/rustc_span/src/source_map.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl SourceMap {
218218
///
219219
/// Unlike `load_file`, guarantees that no normalization like BOM-removal
220220
/// takes place.
221-
pub fn load_binary_file(&self, path: &Path) -> io::Result<Lrc<[u8]>> {
221+
pub fn load_binary_file(&self, path: &Path) -> io::Result<(Lrc<[u8]>, Span)> {
222222
let bytes = self.file_loader.read_binary_file(path)?;
223223

224224
// We need to add file to the `SourceMap`, so that it is present
@@ -227,8 +227,16 @@ impl SourceMap {
227227
// via `mod`, so we try to use real file contents and not just an
228228
// empty string.
229229
let text = std::str::from_utf8(&bytes).unwrap_or("").to_string();
230-
self.new_source_file(path.to_owned().into(), text);
231-
Ok(bytes)
230+
let file = self.new_source_file(path.to_owned().into(), text);
231+
Ok((
232+
bytes,
233+
Span::new(
234+
file.start_pos,
235+
BytePos(file.start_pos.0 + file.source_len.0),
236+
SyntaxContext::root(),
237+
None,
238+
),
239+
))
232240
}
233241

234242
// By returning a `MonotonicVec`, we ensure that consumers cannot invalidate

Diff for: library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
// tidy-alphabetical-start
112112
#![cfg_attr(bootstrap, feature(associated_type_bounds))]
113113
#![feature(array_ptr_get)]
114+
#![feature(asm_experimental_arch)]
114115
#![feature(char_indices_offset)]
115116
#![feature(const_align_of_val)]
116117
#![feature(const_align_of_val_raw)]

Diff for: library/proc_macro/src/bridge/client.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,11 @@ fn maybe_install_panic_hook(force_show_panics: bool) {
283283
HIDE_PANICS_DURING_EXPANSION.call_once(|| {
284284
let prev = panic::take_hook();
285285
panic::set_hook(Box::new(move |info| {
286-
if force_show_panics || !is_available() {
286+
// We normally report panics by catching unwinds and passing the payload from the
287+
// unwind back to the compiler, but if the panic doesn't unwind we'll abort before
288+
// the compiler has a chance to print an error. So we special-case PanicInfo where
289+
// can_unwind is false.
290+
if force_show_panics || !is_available() || !info.can_unwind() {
287291
prev(info)
288292
}
289293
}));

Diff for: library/proc_macro/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#![feature(maybe_uninit_write_slice)]
3131
#![feature(negative_impls)]
3232
#![feature(new_uninit)]
33+
#![feature(panic_can_unwind)]
3334
#![feature(restricted_std)]
3435
#![feature(rustc_attrs)]
3536
#![feature(min_specialization)]

Diff for: library/std/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221
issue = "none",
222222
reason = "You have attempted to use a standard library built for a platform that it doesn't \
223223
know how to support. Consider building it for a known environment, disabling it with \
224-
`#![no_std]` or overriding this warning by enabling this feature".
224+
`#![no_std]` or overriding this warning by enabling this feature."
225225
)
226226
)]
227227
#![cfg_attr(not(bootstrap), rustc_preserve_ub_checks)]

Diff for: src/tools/clippy/clippy_lints/src/large_include_file.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl LateLintPass<'_> for LargeIncludeFile {
7171
span_lint_and_note(
7272
cx,
7373
LARGE_INCLUDE_FILE,
74-
expr.span,
74+
expr.span.source_callsite(),
7575
"attempted to include a large file",
7676
None,
7777
format!(

Diff for: src/tools/clippy/clippy_lints/src/strings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
300300
e.span,
301301
"calling `as_bytes()` on `include_str!(..)`",
302302
"consider using `include_bytes!(..)` instead",
303-
snippet_with_applicability(cx, receiver.span, r#""foo""#, &mut applicability).replacen(
303+
snippet_with_applicability(cx, receiver.span.source_callsite(), r#""foo""#, &mut applicability).replacen(
304304
"include_str",
305305
"include_bytes",
306306
1,

Diff for: src/tools/clippy/tests/ui-toml/large_include_file/large_include_file.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | const TOO_BIG_INCLUDE_BYTES: &[u8; 654] = include_bytes!("too_big.txt");
77
= note: the configuration allows a maximum size of 600 bytes
88
= note: `-D clippy::large-include-file` implied by `-D warnings`
99
= help: to override `-D warnings` add `#[allow(clippy::large_include_file)]`
10-
= note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info)
1110

1211
error: attempted to include a large file
1312
--> tests/ui-toml/large_include_file/large_include_file.rs:14:35
@@ -16,7 +15,6 @@ LL | const TOO_BIG_INCLUDE_STR: &str = include_str!("too_big.txt");
1615
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1716
|
1817
= note: the configuration allows a maximum size of 600 bytes
19-
= note: this error originates in the macro `include_str` (in Nightly builds, run with -Z macro-backtrace for more info)
2018

2119
error: aborting due to 2 previous errors
2220

Diff for: src/tools/clippy/tests/ui/empty_docs.stderr

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@ LL | ///
2525
= help: consider removing or filling it
2626

2727
error: empty doc comment
28-
--> tests/ui/empty_docs.rs:30:5
28+
--> tests/ui/empty_docs.rs:30:13
2929
|
3030
LL | #[doc = ""]
31-
| ^^^^^^^^^^^
31+
| ^^
3232
|
3333
= help: consider removing or filling it
3434

3535
error: empty doc comment
36-
--> tests/ui/empty_docs.rs:33:5
36+
--> tests/ui/empty_docs.rs:33:13
3737
|
38-
LL | / #[doc = ""]
38+
LL | #[doc = ""]
39+
| _____________^
3940
LL | | #[doc = ""]
40-
| |_______________^
41+
| |______________^
4142
|
4243
= help: consider removing or filling it
4344

Diff for: tests/codegen/cffi/c-variadic-naked.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ needs-asm-support
2+
//@ only-x86_64
3+
4+
// tests that `va_start` is not injected into naked functions
5+
6+
#![crate_type = "lib"]
7+
#![feature(c_variadic)]
8+
#![feature(naked_functions)]
9+
#![no_std]
10+
11+
#[naked]
12+
pub unsafe extern "C" fn c_variadic(_: usize, _: ...) {
13+
// CHECK-NOT: va_start
14+
// CHECK-NOT: alloca
15+
core::arch::asm! {
16+
"ret",
17+
options(noreturn),
18+
}
19+
}

0 commit comments

Comments
 (0)