Skip to content

compiletest: Require //~ annotations even if error-pattern is specified #139137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions src/doc/rustc-dev-guide/src/tests/ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ But for strict testing, try to use the `ERROR` annotation as much as possible,
including `//~?` annotations for diagnostics without span.
For compile time diagnostics `error-pattern` should very rarely be necessary.

Per-line annotations (`//~`) are still checked in tests using `error-pattern`,
to opt out of these checks in exceptional cases use `//@ compile-flags: --error-format=human`.

### Error levels

The error levels that you can have are:
Expand Down
16 changes: 6 additions & 10 deletions src/tools/compiletest/src/runtest/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,9 @@ impl TestCx<'_> {
self.props.error_patterns
);

let check_patterns = should_run == WillExecute::No
&& (!self.props.error_patterns.is_empty()
|| !self.props.regex_error_patterns.is_empty());
if !explicit && self.config.compare_mode.is_none() {
let check_annotations = !check_patterns || !expected_errors.is_empty();

if check_annotations {
// "//~ERROR comments"
self.check_expected_errors(expected_errors, &proc_res);
}
// "//~ERROR comments"
self.check_expected_errors(expected_errors, &proc_res);
} else if explicit && !expected_errors.is_empty() {
let msg = format!(
"line {}: cannot combine `--error-format` with {} annotations; use `error-pattern` instead",
Expand All @@ -188,7 +181,10 @@ impl TestCx<'_> {
self.fatal(&msg);
}
let output_to_check = self.get_output(&proc_res);
if check_patterns {
if should_run == WillExecute::No
&& (!self.props.error_patterns.is_empty()
|| !self.props.regex_error_patterns.is_empty())
{
// "// error-pattern" comments
self.check_all_error_patterns(&output_to_check, &proc_res, pm);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/rustdoc-ui/doctest/no-run-flag-error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// test the behavior of the --no-run flag without the --test flag

//@ compile-flags:-Z unstable-options --no-run --test-args=--test-threads=1
//@ error-pattern: the `--test` flag must be passed

pub fn f() {}

//~? ERROR the `--test` flag must be passed to enable `--no-run`
3 changes: 2 additions & 1 deletion tests/rustdoc-ui/invalid-theme-name.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ compile-flags:--theme {{src-base}}/invalid-theme-name.rs
//@ error-pattern: invalid argument
//@ error-pattern: must have a .css extension

//~? ERROR invalid argument: "$DIR/invalid-theme-name.rs"
3 changes: 2 additions & 1 deletion tests/rustdoc-ui/issues/issue-91713.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ check-pass
//@ compile-flags: --passes list
//@ error-pattern: the `passes` flag no longer functions

//~? WARN the `passes` flag no longer functions
3 changes: 2 additions & 1 deletion tests/rustdoc-ui/lints/no-crate-level-doc-lint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@ error-pattern: no documentation found
//@ normalize-stderr: "nightly|beta|1\.[0-9][0-9]\.[0-9]" -> "$$CHANNEL"
#![deny(rustdoc::missing_crate_level_docs)]
//^~ NOTE defined here

pub fn foo() {}

//~? ERROR no documentation found for this crate's top-level module
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/lints/no-crate-level-doc-lint.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ error: no documentation found for this crate's top-level module
= help: The following guide may be of use:
https://doc.rust-lang.org/$CHANNEL/rustdoc/how-to-write-documentation.html
note: the lint level is defined here
--> $DIR/no-crate-level-doc-lint.rs:3:9
--> $DIR/no-crate-level-doc-lint.rs:2:9
|
LL | #![deny(rustdoc::missing_crate_level_docs)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/track-diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
struct A;
struct B;

pub const S: A = B;
pub const S: A = B; //~ ERROR mismatched types
2 changes: 1 addition & 1 deletion tests/ui-fulldeps/missing-rustc-driver-error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Test that we get the following hint when trying to use a compiler crate without rustc_driver.
//@ error-pattern: try adding `extern crate rustc_driver;` at the top level of this crate
//@ compile-flags: --emit link
//@ compile-flags: --emit link --error-format=human
//@ normalize-stderr: ".*crate .* required.*\n\n" -> ""
//@ normalize-stderr: "aborting due to [0-9]+" -> "aborting due to NUMBER"

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/abi/fixed_x18.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@

#[lang = "sized"]
trait Sized {}

//~? ERROR the `-Zfixed-x18` flag is not supported on the `
3 changes: 2 additions & 1 deletion tests/ui/abi/shadow-call-stack-without-fixed-x18.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@ compile-flags: --target aarch64-unknown-none -Zsanitizer=shadow-call-stack
//@ error-pattern: shadow-call-stack sanitizer is not supported for this target
//@ dont-check-compiler-stderr
//@ needs-llvm-components: aarch64

Expand All @@ -13,3 +12,5 @@ trait Sized {}

#[no_mangle]
pub fn foo() {}

//~? ERROR shadow-call-stack sanitizer is not supported for this target
3 changes: 2 additions & 1 deletion tests/ui/allocator/two-allocators2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//@ aux-build:system-allocator.rs
//@ no-prefer-dynamic
//@ error-pattern: the `#[global_allocator]` in

extern crate system_allocator;

Expand All @@ -10,3 +9,5 @@ use std::alloc::System;
static A: System = System;

fn main() {}

//~? ERROR the `#[global_allocator]` in this crate conflicts with global allocator in: system_allocator
4 changes: 2 additions & 2 deletions tests/ui/allocator/two-allocators3.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//@ aux-build:system-allocator.rs
//@ aux-build:system-allocator2.rs
//@ no-prefer-dynamic
//@ error-pattern: the `#[global_allocator]` in


extern crate system_allocator;
extern crate system_allocator2;

fn main() {}

//~? ERROR the `#[global_allocator]` in system_allocator conflicts with global allocator in: system_allocator2
3 changes: 2 additions & 1 deletion tests/ui/amdgpu-require-explicit-cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//@ compile-flags: --crate-type=cdylib --target=amdgcn-amd-amdhsa
//@ needs-llvm-components: amdgpu
//@ needs-rust-lld
//@[nocpu] error-pattern: target requires explicitly specifying a cpu
//@[nocpu] build-fail
//@[cpu] compile-flags: -Ctarget-cpu=gfx900
//@[cpu] build-pass
Expand All @@ -15,3 +14,5 @@
trait Sized {}

pub fn foo() {}

//[nocpu]~? ERROR target requires explicitly specifying a cpu with `-C target-cpu`
2 changes: 1 addition & 1 deletion tests/ui/associated-types/issue-36499.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ error-pattern: aborting due to 1 previous error

fn main() {
2 + +2;
2 + +2; //~ ERROR leading `+` is not supported
}
3 changes: 1 addition & 2 deletions tests/ui/attributes/attr-bad-crate-attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//!
//! See <https://doc.rust-lang.org/reference/attributes.html>.

//@ error-pattern: expected item

#![attr = "val"]
#[attr = "val"] // Unterminated
//~^ ERROR expected item after attributes
2 changes: 1 addition & 1 deletion tests/ui/attributes/attr-bad-crate-attr.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: expected item after attributes
--> $DIR/attr-bad-crate-attr.rs:9:1
--> $DIR/attr-bad-crate-attr.rs:7:1
|
LL | #[attr = "val"] // Unterminated
| ^^^^^^^^^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/attributes/z-crate-attr/garbage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Show diagnostics for invalid tokens
//@ compile-flags: -Zcrate-attr=`%~@$#
//@ error-pattern:unknown start of token

fn main() {}

//~? ERROR unknown start of token: `
//~? ERROR expected identifier, found `%`
4 changes: 3 additions & 1 deletion tests/ui/attributes/z-crate-attr/injection.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ compile-flags: '-Zcrate-attr=feature(yeet_expr)]fn main(){}#[inline'
//@ error-pattern:unexpected closing delimiter

fn foo() {}

//~? ERROR unexpected closing delimiter: `]`
3 changes: 2 additions & 1 deletion tests/ui/attributes/z-crate-attr/inner-attr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@ compile-flags: -Zcrate-attr=#![feature(foo)]
//@ error-pattern:expected identifier

fn main() {}

//~? ERROR expected identifier, found `#`
4 changes: 3 additions & 1 deletion tests/ui/attributes/z-crate-attr/multiple.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ compile-flags: -Zcrate-attr=feature(foo),feature(bar)
//@ error-pattern:invalid crate attr

fn main() {}

//~? ERROR invalid crate attribute
4 changes: 3 additions & 1 deletion tests/ui/attributes/z-crate-attr/unbalanced-paren.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Show diagnostics for unbalanced parens.
//@ compile-flags: -Zcrate-attr=(
//@ error-pattern:unclosed delimiter

fn main() {}

//~? ERROR this file contains an unclosed delimiter
3 changes: 1 addition & 2 deletions tests/ui/binop/binop-bitxor-str.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
//@ error-pattern:no implementation for `String ^ String`

fn main() { let x = "a".to_string() ^ "b".to_string(); }
//~^ ERROR no implementation for `String ^ String`
2 changes: 1 addition & 1 deletion tests/ui/binop/binop-bitxor-str.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0369]: no implementation for `String ^ String`
--> $DIR/binop-bitxor-str.rs:3:37
--> $DIR/binop-bitxor-str.rs:1:37
|
LL | fn main() { let x = "a".to_string() ^ "b".to_string(); }
| --------------- ^ --------------- String
Expand Down
4 changes: 1 addition & 3 deletions tests/ui/binop/binop-mul-bool.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
//@ error-pattern:cannot multiply `bool` by `bool`

fn main() { let x = true * false; }
fn main() { let x = true * false; } //~ ERROR cannot multiply `bool` by `bool`
2 changes: 1 addition & 1 deletion tests/ui/binop/binop-mul-bool.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0369]: cannot multiply `bool` by `bool`
--> $DIR/binop-mul-bool.rs:3:26
--> $DIR/binop-mul-bool.rs:1:26
|
LL | fn main() { let x = true * false; }
| ---- ^ ----- bool
Expand Down
14 changes: 10 additions & 4 deletions tests/ui/c-variadic/issue-86053-1.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// Regression test for the ICE described in issue #86053.
//@ error-pattern:unexpected `self` parameter in function
//@ error-pattern:`...` must be the last argument of a C-variadic function
//@ error-pattern:cannot find type `F` in this scope


#![feature(c_variadic)]
#![crate_type="lib"]

fn ordering4 < 'a , 'b > ( a : , self , self , self ,
//~^ ERROR expected type, found `,`
//~| ERROR unexpected `self` parameter in function
//~| ERROR unexpected `self` parameter in function
//~| ERROR unexpected `self` parameter in function
self , ... , self , self , ... ) where F : FnOnce ( & 'a & 'b usize ) {
//~^ ERROR unexpected `self` parameter in function
//~| ERROR unexpected `self` parameter in function
//~| ERROR unexpected `self` parameter in function
//~| ERROR `...` must be the last argument of a C-variadic function
//~| ERROR only foreign, `unsafe extern "C"`, or `unsafe extern "C-unwind"` functions may have a C-variadic arg
//~| ERROR cannot find type `F` in this scope
}
8 changes: 4 additions & 4 deletions tests/ui/c-variadic/issue-86053-1.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
error: expected type, found `,`
--> $DIR/issue-86053-1.rs:10:47
--> $DIR/issue-86053-1.rs:6:47
|
LL | fn ordering4 < 'a , 'b > ( a : , self , self , self ,
| ^ expected type

error: unexpected `self` parameter in function
--> $DIR/issue-86053-1.rs:10:51
--> $DIR/issue-86053-1.rs:6:51
|
LL | fn ordering4 < 'a , 'b > ( a : , self , self , self ,
| ^^^^ must be the first parameter of an associated function

error: unexpected `self` parameter in function
--> $DIR/issue-86053-1.rs:10:58
--> $DIR/issue-86053-1.rs:6:58
|
LL | fn ordering4 < 'a , 'b > ( a : , self , self , self ,
| ^^^^ must be the first parameter of an associated function

error: unexpected `self` parameter in function
--> $DIR/issue-86053-1.rs:10:67
--> $DIR/issue-86053-1.rs:6:67
|
LL | fn ordering4 < 'a , 'b > ( a : , self , self , self ,
| ^^^^ must be the first parameter of an associated function
Expand Down
4 changes: 1 addition & 3 deletions tests/ui/capture1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//@ error-pattern: can't capture dynamic environment in a fn item

fn main() {
let bar: isize = 5;
fn foo() -> isize { return bar; }
fn foo() -> isize { return bar; } //~ ERROR can't capture dynamic environment in a fn item
}
2 changes: 1 addition & 1 deletion tests/ui/capture1.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0434]: can't capture dynamic environment in a fn item
--> $DIR/capture1.rs:5:32
--> $DIR/capture1.rs:3:32
|
LL | fn foo() -> isize { return bar; }
| ^^^
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/cast/cast-from-nil.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
//@ error-pattern: non-primitive cast: `()` as `u32`
fn main() { let u = (assert!(true) as u32); }
fn main() { let u = (assert!(true) as u32); } //~ ERROR non-primitive cast: `()` as `u32`
2 changes: 1 addition & 1 deletion tests/ui/cast/cast-from-nil.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0605]: non-primitive cast: `()` as `u32`
--> $DIR/cast-from-nil.rs:2:21
--> $DIR/cast-from-nil.rs:1:21
|
LL | fn main() { let u = (assert!(true) as u32); }
| ^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/cast/cast-to-nil.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
//@ error-pattern: non-primitive cast: `u32` as `()`
fn main() { let u = 0u32 as (); }
fn main() { let u = 0u32 as (); } //~ ERROR non-primitive cast: `u32` as `()`
2 changes: 1 addition & 1 deletion tests/ui/cast/cast-to-nil.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0605]: non-primitive cast: `u32` as `()`
--> $DIR/cast-to-nil.rs:2:21
--> $DIR/cast-to-nil.rs:1:21
|
LL | fn main() { let u = 0u32 as (); }
| ^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
Expand Down
7 changes: 6 additions & 1 deletion tests/ui/cfg/cfg_false_no_std-2.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// Error, the linked empty library is `no_std` and doesn't provide a panic handler.

//@ dont-check-compiler-stderr
//@ compile-flags: --error-format=human
//@ error-pattern: `#[panic_handler]` function required, but not found
//@ dont-check-compiler-stderr
//@ aux-build: cfg_false_lib_no_std_before.rs

#![no_std]

extern crate cfg_false_lib_no_std_before as _;

fn main() {}

// FIXME: The second error is target-dependent.
//FIXME~? ERROR `#[panic_handler]` function required, but not found
//FIXME~? ERROR unwinding panics are not supported without std
3 changes: 2 additions & 1 deletion tests/ui/codegen/mismatched-data-layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//@ build-fail
//@ needs-llvm-components: x86
//@ compile-flags: --crate-type=lib --target={{src-base}}/codegen/mismatched-data-layout.json -Z unstable-options
//@ error-pattern: differs from LLVM target's
//@ normalize-stderr: "`, `[A-Za-z0-9-:]*`" -> "`, `normalized data layout`"
//@ normalize-stderr: "layout, `[A-Za-z0-9-:]*`" -> "layout, `normalized data layout`"

Expand All @@ -12,3 +11,5 @@

#[lang = "sized"]
trait Sized {}

//~? ERROR differs from LLVM target's
4 changes: 3 additions & 1 deletion tests/ui/conditional-compilation/cfg-arg-invalid-3.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ compile-flags: --cfg a::b
//@ error-pattern: invalid `--cfg` argument: `a::b` (argument key must be an identifier)

fn main() {}

//~? ERROR invalid `--cfg` argument: `a::b` (argument key must be an identifier)
4 changes: 3 additions & 1 deletion tests/ui/conditional-compilation/cfg-arg-invalid-5.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ compile-flags: --cfg a=10
//@ error-pattern: invalid `--cfg` argument: `a=10` (argument value must be a string)

fn main() {}

//~? ERROR invalid `--cfg` argument: `a=10` (argument value must be a string)
3 changes: 1 addition & 2 deletions tests/ui/conditional-compilation/cfg-attr-cfg-2.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//@ error-pattern: `main` function not found
//@ compile-flags: --cfg foo --check-cfg=cfg(foo,bar)

// main is conditionally compiled, but the conditional compilation
// is conditional too!

#[cfg_attr(foo, cfg(bar))]
fn main() { }
fn main() { } //~ ERROR `main` function not found in crate `cfg_attr_cfg_2`
2 changes: 1 addition & 1 deletion tests/ui/conditional-compilation/cfg-attr-cfg-2.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0601]: `main` function not found in crate `cfg_attr_cfg_2`
--> $DIR/cfg-attr-cfg-2.rs:8:14
--> $DIR/cfg-attr-cfg-2.rs:7:14
|
LL | fn main() { }
| ^ consider adding a `main` function to `$DIR/cfg-attr-cfg-2.rs`
Expand Down
4 changes: 1 addition & 3 deletions tests/ui/conditional-compilation/cfg-in-crate-1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
//@ error-pattern: `main` function not found

#![cfg(FALSE)]
#![cfg(FALSE)] //~ ERROR `main` function not found in crate `cfg_in_crate_1`
2 changes: 1 addition & 1 deletion tests/ui/conditional-compilation/cfg-in-crate-1.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0601]: `main` function not found in crate `cfg_in_crate_1`
--> $DIR/cfg-in-crate-1.rs:3:15
--> $DIR/cfg-in-crate-1.rs:1:15
|
LL | #![cfg(FALSE)]
| ^ consider adding a `main` function to `$DIR/cfg-in-crate-1.rs`
Expand Down
Loading
Loading