Skip to content

Commit abf3ec5

Browse files
committed
Auto merge of rust-lang#85057 - Dylan-DPC:rollup-efaseq2, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - rust-lang#76808 (Improve diagnostics for functions in `struct` definitions) - rust-lang#84887 (Remove SpanInterner::get) - rust-lang#85034 (fix null pointer error messages) - rust-lang#85038 (Don't stop running rustdoc-gui tests at first failure) - rust-lang#85044 (Use `path.exists()` instead of `fs::metadata(path).is_ok()`) - rust-lang#85052 (rustdoc: Link to the docs on namespaces when an unknown disambiguator is found) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 467253f + 8852317 commit abf3ec5

File tree

16 files changed

+191
-96
lines changed

16 files changed

+191
-96
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ impl<'a> Linker for MsvcLinker<'a> {
772772
// check to see if the file is there and just omit linking to it if it's
773773
// not present.
774774
let name = format!("{}.dll.lib", lib);
775-
if fs::metadata(&path.join(&name)).is_ok() {
775+
if path.join(&name).exists() {
776776
self.cmd.arg(name);
777777
}
778778
}

compiler/rustc_middle/src/mir/interpret/error.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,25 @@ impl fmt::Display for InvalidProgramInfo<'_> {
170170
/// Details of why a pointer had to be in-bounds.
171171
#[derive(Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
172172
pub enum CheckInAllocMsg {
173+
/// We are access memory.
173174
MemoryAccessTest,
175+
/// We are doing pointer arithmetic.
174176
PointerArithmeticTest,
177+
/// None of the above -- generic/unspecific inbounds test.
175178
InboundsTest,
176179
}
177180

178181
impl fmt::Display for CheckInAllocMsg {
179182
/// When this is printed as an error the context looks like this
180-
/// "{test name} failed: pointer must be in-bounds at offset..."
183+
/// "{msg}pointer must be in-bounds at offset..."
181184
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182185
write!(
183186
f,
184187
"{}",
185188
match *self {
186-
CheckInAllocMsg::MemoryAccessTest => "memory access",
187-
CheckInAllocMsg::PointerArithmeticTest => "pointer arithmetic",
188-
CheckInAllocMsg::InboundsTest => "inbounds test",
189+
CheckInAllocMsg::MemoryAccessTest => "memory access failed: ",
190+
CheckInAllocMsg::PointerArithmeticTest => "pointer arithmetic failed: ",
191+
CheckInAllocMsg::InboundsTest => "",
189192
}
190193
)
191194
}
@@ -299,18 +302,18 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
299302
}
300303
PointerOutOfBounds { ptr, msg, allocation_size } => write!(
301304
f,
302-
"{} failed: pointer must be in-bounds at offset {}, \
305+
"{}pointer must be in-bounds at offset {}, \
303306
but is outside bounds of {} which has size {}",
304307
msg,
305308
ptr.offset.bytes(),
306309
ptr.alloc_id,
307310
allocation_size.bytes()
308311
),
309-
DanglingIntPointer(_, CheckInAllocMsg::InboundsTest) => {
310-
write!(f, "null pointer is not allowed for this operation")
312+
DanglingIntPointer(0, CheckInAllocMsg::InboundsTest) => {
313+
write!(f, "null pointer is not a valid pointer for this operation")
311314
}
312315
DanglingIntPointer(i, msg) => {
313-
write!(f, "{} failed: 0x{:x} is not a valid pointer", msg, i)
316+
write!(f, "{}0x{:x} is not a valid pointer", msg, i)
314317
}
315318
AlignmentCheckFailed { required, has } => write!(
316319
f,

compiler/rustc_parse/src/parser/item.rs

+40-12
Original file line numberDiff line numberDiff line change
@@ -1124,11 +1124,11 @@ impl<'a> Parser<'a> {
11241124
if !this.recover_nested_adt_item(kw::Enum)? {
11251125
return Ok((None, TrailingToken::None));
11261126
}
1127-
let ident = this.parse_ident()?;
1127+
let ident = this.parse_field_ident("enum", vlo)?;
11281128

11291129
let struct_def = if this.check(&token::OpenDelim(token::Brace)) {
11301130
// Parse a struct variant.
1131-
let (fields, recovered) = this.parse_record_struct_body()?;
1131+
let (fields, recovered) = this.parse_record_struct_body("struct")?;
11321132
VariantData::Struct(fields, recovered)
11331133
} else if this.check(&token::OpenDelim(token::Paren)) {
11341134
VariantData::Tuple(this.parse_tuple_struct_body()?, DUMMY_NODE_ID)
@@ -1182,15 +1182,15 @@ impl<'a> Parser<'a> {
11821182
VariantData::Unit(DUMMY_NODE_ID)
11831183
} else {
11841184
// If we see: `struct Foo<T> where T: Copy { ... }`
1185-
let (fields, recovered) = self.parse_record_struct_body()?;
1185+
let (fields, recovered) = self.parse_record_struct_body("struct")?;
11861186
VariantData::Struct(fields, recovered)
11871187
}
11881188
// No `where` so: `struct Foo<T>;`
11891189
} else if self.eat(&token::Semi) {
11901190
VariantData::Unit(DUMMY_NODE_ID)
11911191
// Record-style struct definition
11921192
} else if self.token == token::OpenDelim(token::Brace) {
1193-
let (fields, recovered) = self.parse_record_struct_body()?;
1193+
let (fields, recovered) = self.parse_record_struct_body("struct")?;
11941194
VariantData::Struct(fields, recovered)
11951195
// Tuple-style struct definition with optional where-clause.
11961196
} else if self.token == token::OpenDelim(token::Paren) {
@@ -1220,10 +1220,10 @@ impl<'a> Parser<'a> {
12201220

12211221
let vdata = if self.token.is_keyword(kw::Where) {
12221222
generics.where_clause = self.parse_where_clause()?;
1223-
let (fields, recovered) = self.parse_record_struct_body()?;
1223+
let (fields, recovered) = self.parse_record_struct_body("union")?;
12241224
VariantData::Struct(fields, recovered)
12251225
} else if self.token == token::OpenDelim(token::Brace) {
1226-
let (fields, recovered) = self.parse_record_struct_body()?;
1226+
let (fields, recovered) = self.parse_record_struct_body("union")?;
12271227
VariantData::Struct(fields, recovered)
12281228
} else {
12291229
let token_str = super::token_descr(&self.token);
@@ -1236,12 +1236,15 @@ impl<'a> Parser<'a> {
12361236
Ok((class_name, ItemKind::Union(vdata, generics)))
12371237
}
12381238

1239-
fn parse_record_struct_body(&mut self) -> PResult<'a, (Vec<FieldDef>, /* recovered */ bool)> {
1239+
fn parse_record_struct_body(
1240+
&mut self,
1241+
adt_ty: &str,
1242+
) -> PResult<'a, (Vec<FieldDef>, /* recovered */ bool)> {
12401243
let mut fields = Vec::new();
12411244
let mut recovered = false;
12421245
if self.eat(&token::OpenDelim(token::Brace)) {
12431246
while self.token != token::CloseDelim(token::Brace) {
1244-
let field = self.parse_field_def().map_err(|e| {
1247+
let field = self.parse_field_def(adt_ty).map_err(|e| {
12451248
self.consume_block(token::Brace, ConsumeClosingDelim::No);
12461249
recovered = true;
12471250
e
@@ -1294,24 +1297,25 @@ impl<'a> Parser<'a> {
12941297
}
12951298

12961299
/// Parses an element of a struct declaration.
1297-
fn parse_field_def(&mut self) -> PResult<'a, FieldDef> {
1300+
fn parse_field_def(&mut self, adt_ty: &str) -> PResult<'a, FieldDef> {
12981301
let attrs = self.parse_outer_attributes()?;
12991302
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
13001303
let lo = this.token.span;
13011304
let vis = this.parse_visibility(FollowedByType::No)?;
1302-
Ok((this.parse_single_struct_field(lo, vis, attrs)?, TrailingToken::None))
1305+
Ok((this.parse_single_struct_field(adt_ty, lo, vis, attrs)?, TrailingToken::None))
13031306
})
13041307
}
13051308

13061309
/// Parses a structure field declaration.
13071310
fn parse_single_struct_field(
13081311
&mut self,
1312+
adt_ty: &str,
13091313
lo: Span,
13101314
vis: Visibility,
13111315
attrs: Vec<Attribute>,
13121316
) -> PResult<'a, FieldDef> {
13131317
let mut seen_comma: bool = false;
1314-
let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
1318+
let a_var = self.parse_name_and_ty(adt_ty, lo, vis, attrs)?;
13151319
if self.token == token::Comma {
13161320
seen_comma = true;
13171321
}
@@ -1398,11 +1402,12 @@ impl<'a> Parser<'a> {
13981402
/// Parses a structure field.
13991403
fn parse_name_and_ty(
14001404
&mut self,
1405+
adt_ty: &str,
14011406
lo: Span,
14021407
vis: Visibility,
14031408
attrs: Vec<Attribute>,
14041409
) -> PResult<'a, FieldDef> {
1405-
let name = self.parse_ident_common(false)?;
1410+
let name = self.parse_field_ident(adt_ty, lo)?;
14061411
self.expect(&token::Colon)?;
14071412
let ty = self.parse_ty()?;
14081413
Ok(FieldDef {
@@ -1416,6 +1421,29 @@ impl<'a> Parser<'a> {
14161421
})
14171422
}
14181423

1424+
/// Parses a field identifier. Specialized version of `parse_ident_common`
1425+
/// for better diagnostics and suggestions.
1426+
fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
1427+
let (ident, is_raw) = self.ident_or_err()?;
1428+
if !is_raw && ident.is_reserved() {
1429+
let err = if self.check_fn_front_matter(false) {
1430+
let _ = self.parse_fn(&mut Vec::new(), |_| true, lo);
1431+
let mut err = self.struct_span_err(
1432+
lo.to(self.prev_token.span),
1433+
&format!("functions are not allowed in {} definitions", adt_ty),
1434+
);
1435+
err.help("unlike in C++, Java, and C#, functions are declared in `impl` blocks");
1436+
err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information");
1437+
err
1438+
} else {
1439+
self.expected_ident_found()
1440+
};
1441+
return Err(err);
1442+
}
1443+
self.bump();
1444+
Ok(ident)
1445+
}
1446+
14191447
/// Parses a declarative macro 2.0 definition.
14201448
/// The `macro` keyword has already been parsed.
14211449
/// ```

compiler/rustc_parse/src/parser/mod.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -522,27 +522,27 @@ impl<'a> Parser<'a> {
522522
self.parse_ident_common(true)
523523
}
524524

525+
fn ident_or_err(&mut self) -> PResult<'a, (Ident, /* is_raw */ bool)> {
526+
self.token.ident().ok_or_else(|| match self.prev_token.kind {
527+
TokenKind::DocComment(..) => {
528+
self.span_fatal_err(self.prev_token.span, Error::UselessDocComment)
529+
}
530+
_ => self.expected_ident_found(),
531+
})
532+
}
533+
525534
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
526-
match self.token.ident() {
527-
Some((ident, is_raw)) => {
528-
if !is_raw && ident.is_reserved() {
529-
let mut err = self.expected_ident_found();
530-
if recover {
531-
err.emit();
532-
} else {
533-
return Err(err);
534-
}
535-
}
536-
self.bump();
537-
Ok(ident)
535+
let (ident, is_raw) = self.ident_or_err()?;
536+
if !is_raw && ident.is_reserved() {
537+
let mut err = self.expected_ident_found();
538+
if recover {
539+
err.emit();
540+
} else {
541+
return Err(err);
538542
}
539-
_ => Err(match self.prev_token.kind {
540-
TokenKind::DocComment(..) => {
541-
self.span_fatal_err(self.prev_token.span, Error::UselessDocComment)
542-
}
543-
_ => self.expected_ident_found(),
544-
}),
545543
}
544+
self.bump();
545+
Ok(ident)
546546
}
547547

548548
/// Checks if the next token is `tok`, and returns `true` if so.

compiler/rustc_span/src/source_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub struct RealFileLoader;
109109

110110
impl FileLoader for RealFileLoader {
111111
fn file_exists(&self, path: &Path) -> bool {
112-
fs::metadata(path).is_ok()
112+
path.exists()
113113
}
114114

115115
fn read_file(&self, path: &Path) -> io::Result<String> {

compiler/rustc_span/src/span_encoding.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl Span {
102102
// Interned format.
103103
debug_assert!(self.ctxt_or_zero == 0);
104104
let index = self.base_or_index;
105-
with_span_interner(|interner| *interner.get(index))
105+
with_span_interner(|interner| interner.spans[index as usize])
106106
}
107107
}
108108
}
@@ -117,11 +117,6 @@ impl SpanInterner {
117117
let (index, _) = self.spans.insert_full(*span_data);
118118
index as u32
119119
}
120-
121-
#[inline]
122-
fn get(&self, index: u32) -> &SpanData {
123-
&self.spans[index as usize]
124-
}
125120
}
126121

127122
// If an interner exists, return it. Otherwise, prepare a fresh one.

src/bootstrap/test.rs

+8-22
Original file line numberDiff line numberDiff line change
@@ -831,28 +831,14 @@ impl Step for RustdocGUI {
831831
command.arg("src/test/rustdoc-gui/lib.rs").arg("-o").arg(&out_dir);
832832
builder.run(&mut command);
833833

834-
let mut tests = Vec::new();
835-
for file in fs::read_dir("src/test/rustdoc-gui").unwrap() {
836-
let file = file.unwrap();
837-
let file_path = file.path();
838-
let file_name = file.file_name();
839-
840-
if !file_name.to_str().unwrap().ends_with(".goml") {
841-
continue;
842-
}
843-
tests.push(file_path);
844-
}
845-
tests.sort_unstable();
846-
for test in tests {
847-
let mut command = Command::new(&nodejs);
848-
command
849-
.arg("src/tools/rustdoc-gui/tester.js")
850-
.arg("--doc-folder")
851-
.arg(out_dir.join("test_docs"))
852-
.arg("--test-file")
853-
.arg(test);
854-
builder.run(&mut command);
855-
}
834+
let mut command = Command::new(&nodejs);
835+
command
836+
.arg("src/tools/rustdoc-gui/tester.js")
837+
.arg("--doc-folder")
838+
.arg(out_dir.join("test_docs"))
839+
.arg("--tests-folder")
840+
.arg("src/test/rustdoc-gui");
841+
builder.run(&mut command);
856842
} else {
857843
builder.info("No nodejs found, skipping \"src/test/rustdoc-gui\" tests");
858844
}

src/librustdoc/passes/collect_intra_doc_links.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2017,7 +2017,10 @@ fn disambiguator_error(
20172017
msg: &str,
20182018
) {
20192019
diag_info.link_range = disambiguator_range;
2020-
report_diagnostic(cx.tcx, BROKEN_INTRA_DOC_LINKS, msg, &diag_info, |_diag, _sp| {});
2020+
report_diagnostic(cx.tcx, BROKEN_INTRA_DOC_LINKS, msg, &diag_info, |diag, _sp| {
2021+
let msg = "see https://doc.rust-lang.org/nightly/rustdoc/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators";
2022+
diag.note(msg);
2023+
});
20212024
}
20222025

20232026
/// Report an ambiguity error, where there were multiple possible resolutions.

src/test/rustdoc-ui/intra-doc/email-address-localhost.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ note: the lint level is defined here
1010
LL | #![deny(warnings)]
1111
| ^^^^^^^^
1212
= note: `#[deny(rustdoc::broken_intra_doc_links)]` implied by `#[deny(warnings)]`
13+
= note: see https://doc.rust-lang.org/nightly/rustdoc/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
1314

1415
error: aborting due to previous error
1516

src/test/rustdoc-ui/intra-doc/unknown-disambiguator.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,47 @@ note: the lint level is defined here
1010
LL | #![deny(warnings)]
1111
| ^^^^^^^^
1212
= note: `#[deny(rustdoc::broken_intra_doc_links)]` implied by `#[deny(warnings)]`
13+
= note: see https://doc.rust-lang.org/nightly/rustdoc/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
1314

1415
error: unknown disambiguator `bar`
1516
--> $DIR/unknown-disambiguator.rs:3:35
1617
|
1718
LL | //! Linking to [foo@banana] and [`bar@banana!()`].
1819
| ^^^
20+
|
21+
= note: see https://doc.rust-lang.org/nightly/rustdoc/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
1922

2023
error: unknown disambiguator `foo`
2124
--> $DIR/unknown-disambiguator.rs:9:34
2225
|
2326
LL | //! And with weird backticks: [``foo@hello``] [foo`@`hello].
2427
| ^^^
28+
|
29+
= note: see https://doc.rust-lang.org/nightly/rustdoc/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
2530

2631
error: unknown disambiguator `foo`
2732
--> $DIR/unknown-disambiguator.rs:9:48
2833
|
2934
LL | //! And with weird backticks: [``foo@hello``] [foo`@`hello].
3035
| ^^^
36+
|
37+
= note: see https://doc.rust-lang.org/nightly/rustdoc/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
3138

3239
error: unknown disambiguator ``
3340
--> $DIR/unknown-disambiguator.rs:6:31
3441
|
3542
LL | //! And to [no disambiguator](@nectarine) and [another](@apricot!()).
3643
| ^
44+
|
45+
= note: see https://doc.rust-lang.org/nightly/rustdoc/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
3746

3847
error: unknown disambiguator ``
3948
--> $DIR/unknown-disambiguator.rs:6:57
4049
|
4150
LL | //! And to [no disambiguator](@nectarine) and [another](@apricot!()).
4251
| ^
52+
|
53+
= note: see https://doc.rust-lang.org/nightly/rustdoc/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators
4354

4455
error: aborting due to 6 previous errors
4556

src/test/ui/consts/const-eval/ub-wide-ptr.32bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ error[E0080]: could not evaluate static initializer
296296
--> $DIR/ub-wide-ptr.rs:135:5
297297
|
298298
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
299-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not allowed for this operation
299+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation
300300

301301
error[E0080]: could not evaluate static initializer
302302
--> $DIR/ub-wide-ptr.rs:139:5

src/test/ui/consts/const-eval/ub-wide-ptr.64bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ error[E0080]: could not evaluate static initializer
296296
--> $DIR/ub-wide-ptr.rs:135:5
297297
|
298298
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
299-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not allowed for this operation
299+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation
300300

301301
error[E0080]: could not evaluate static initializer
302302
--> $DIR/ub-wide-ptr.rs:139:5

0 commit comments

Comments
 (0)