Skip to content

Commit 3b5afa5

Browse files
author
Yuki Okushi
authored
Rollup merge of #106557 - Ezrashaw:ui-test-fixups-1, r=GuillaumeGomez
Add some UI tests and reword error-code docs Added UI tests for `E0013` and `E0015`. Error code docs for `E0015` were a bit unclear (they referred to all non-const errors in const context, when only non-const functions applied), so I touched them up a bit. I also fixed up some issues in the new `error_codes.rs` tidy check (linked #106341), that I overlooked previously. r? ``@GuillaumeGomez``
2 parents 7997ff6 + ae61c25 commit 3b5afa5

File tree

6 files changed

+57
-27
lines changed

6 files changed

+57
-27
lines changed
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
A constant item was initialized with something that is not a constant
2-
expression.
1+
A non-`const` function was called in a `const` context.
32

43
Erroneous code example:
54

@@ -8,26 +7,20 @@ fn create_some() -> Option<u8> {
87
Some(1)
98
}
109
11-
const FOO: Option<u8> = create_some(); // error!
10+
// error: cannot call non-const fn `create_some` in constants
11+
const FOO: Option<u8> = create_some();
1212
```
1313

14-
The only functions that can be called in static or constant expressions are
15-
`const` functions, and struct/enum constructors.
14+
All functions used in a `const` context (constant or static expression) must
15+
be marked `const`.
1616

1717
To fix this error, you can declare `create_some` as a constant function:
1818

1919
```
20-
const fn create_some() -> Option<u8> { // declared as a const function
20+
// declared as a `const` function:
21+
const fn create_some() -> Option<u8> {
2122
Some(1)
2223
}
2324
24-
const FOO: Option<u8> = create_some(); // ok!
25-
26-
// These are also working:
27-
struct Bar {
28-
x: u8,
29-
}
30-
31-
const OTHER_FOO: Option<u8> = Some(1);
32-
const BAR: Bar = Bar {x: 1};
25+
const FOO: Option<u8> = create_some(); // no error!
3326
```

src/test/ui/error-codes/E0013.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
static X: i32 = 42;
2+
const Y: i32 = X; //~ ERROR constants cannot refer to statics [E0013]
3+
4+
fn main() {}

src/test/ui/error-codes/E0013.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0013]: constants cannot refer to statics
2+
--> $DIR/E0013.rs:2:16
3+
|
4+
LL | const Y: i32 = X;
5+
| ^
6+
|
7+
= help: consider extracting the value of the `static` to a `const`, and referring to that
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0013`.

src/test/ui/error-codes/E0015.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn create_some() -> Option<u8> {
2+
Some(1)
3+
}
4+
5+
const FOO: Option<u8> = create_some();
6+
//~^ ERROR cannot call non-const fn `create_some` in constants [E0015]
7+
8+
fn main() {}

src/test/ui/error-codes/E0015.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0015]: cannot call non-const fn `create_some` in constants
2+
--> $DIR/E0015.rs:5:25
3+
|
4+
LL | const FOO: Option<u8> = create_some();
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0015`.

src/tools/tidy/src/error_codes.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
//!
1616
//! 4. We check that the error code is actually emitted by the compiler.
1717
//! - This is done by searching `compiler/` with a regex.
18-
//!
19-
//! This tidy check was merged and refactored from two others. See #PR_NUM for information about linting changes that occurred during this refactor.
2018
2119
use std::{ffi::OsStr, fs, path::Path};
2220

@@ -57,7 +55,7 @@ pub fn check(root_path: &Path, search_paths: &[&Path], verbose: bool, bad: &mut
5755
let no_longer_emitted = check_error_codes_docs(root_path, &error_codes, &mut errors, verbose);
5856

5957
// Stage 3: check list has UI tests
60-
check_error_codes_tests(root_path, &error_codes, &mut errors, verbose);
58+
check_error_codes_tests(root_path, &error_codes, &mut errors, verbose, &no_longer_emitted);
6159

6260
// Stage 4: check list is emitted by compiler
6361
check_error_codes_used(search_paths, &error_codes, &mut errors, &no_longer_emitted, verbose);
@@ -174,22 +172,21 @@ fn check_error_codes_docs(
174172
return;
175173
}
176174

177-
let (found_code_example, found_proper_doctest, emit_ignore_warning, emit_no_longer_warning) =
175+
let (found_code_example, found_proper_doctest, emit_ignore_warning, no_longer_emitted) =
178176
check_explanation_has_doctest(&contents, &err_code);
177+
179178
if emit_ignore_warning {
180179
verbose_print!(
181180
verbose,
182181
"warning: Error code `{err_code}` uses the ignore header. This should not be used, add the error code to the \
183182
`IGNORE_DOCTEST_CHECK` constant instead."
184183
);
185184
}
186-
if emit_no_longer_warning {
185+
186+
if no_longer_emitted {
187187
no_longer_emitted_codes.push(err_code.to_owned());
188-
verbose_print!(
189-
verbose,
190-
"warning: Error code `{err_code}` is no longer emitted and should be removed entirely."
191-
);
192188
}
189+
193190
if !found_code_example {
194191
verbose_print!(
195192
verbose,
@@ -226,7 +223,7 @@ fn check_explanation_has_doctest(explanation: &str, err_code: &str) -> (bool, bo
226223
let mut found_proper_doctest = false;
227224

228225
let mut emit_ignore_warning = false;
229-
let mut emit_no_longer_warning = false;
226+
let mut no_longer_emitted = false;
230227

231228
for line in explanation.lines() {
232229
let line = line.trim();
@@ -246,13 +243,13 @@ fn check_explanation_has_doctest(explanation: &str, err_code: &str) -> (bool, bo
246243
} else if line
247244
.starts_with("#### Note: this error code is no longer emitted by the compiler")
248245
{
249-
emit_no_longer_warning = true;
246+
no_longer_emitted = true;
250247
found_code_example = true;
251248
found_proper_doctest = true;
252249
}
253250
}
254251

255-
(found_code_example, found_proper_doctest, emit_ignore_warning, emit_no_longer_warning)
252+
(found_code_example, found_proper_doctest, emit_ignore_warning, no_longer_emitted)
256253
}
257254

258255
// Stage 3: Checks that each error code has a UI test in the correct directory
@@ -261,6 +258,7 @@ fn check_error_codes_tests(
261258
error_codes: &[String],
262259
errors: &mut Vec<String>,
263260
verbose: bool,
261+
no_longer_emitted: &[String],
264262
) {
265263
let tests_path = root_path.join(Path::new(ERROR_TESTS_PATH));
266264

@@ -295,6 +293,11 @@ fn check_error_codes_tests(
295293
}
296294
};
297295

296+
if no_longer_emitted.contains(code) {
297+
// UI tests *can't* contain error codes that are no longer emitted.
298+
continue;
299+
}
300+
298301
let mut found_code = false;
299302

300303
for line in file.lines() {

0 commit comments

Comments
 (0)