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

compiler/rustc_ast_passes/messages.ftl

Lines changed: 1 addition & 0 deletions
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

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 5 additions & 1 deletion
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)) {

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 6 additions & 0 deletions
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)]

compiler/rustc_builtin_macros/src/source_util.rs

Lines changed: 6 additions & 4 deletions
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) => {

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 7 additions & 0 deletions
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()

compiler/rustc_error_codes/src/error_codes/E0384.md

Lines changed: 13 additions & 0 deletions
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+
```

compiler/rustc_errors/src/emitter.rs

Lines changed: 7 additions & 2 deletions
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);

compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 7 additions & 13 deletions
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(

compiler/rustc_resolve/src/rustdoc.rs

Lines changed: 38 additions & 7 deletions
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 {

compiler/rustc_span/src/source_map.rs

Lines changed: 11 additions & 3 deletions
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

0 commit comments

Comments
 (0)