Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,19 @@ impl Drop for DiagCtxtInner {
}
}

thread_local! {
/// Number of errors emitted on the current thread.
///
/// The shared error count ([`DiagCtxt::err_count`]) is bumped by every
/// thread, so a before/after delta taken around one unit of work can be
/// polluted by errors emitted concurrently on other threads. The
/// monomorphization collector relies on such a delta to attribute
/// post-monomorphization errors to the right item, and got confused when
/// collecting in parallel (see #154260). This per-thread count lets it take
/// a delta that is not affected by other threads.
static THREAD_ERR_COUNT: Cell<usize> = const { Cell::new(0) };
}

impl DiagCtxt {
pub fn disable_warnings(mut self) -> Self {
self.inner.get_mut().flags.can_emit_warnings = false;
Expand Down Expand Up @@ -714,6 +727,19 @@ impl<'a> DiagCtxtHandle<'a> {
.sum::<usize>()
}

/// The number of errors that have been emitted on the *current thread*.
///
/// Unlike [`DiagCtxtHandle::err_count`], which counts errors emitted by all
/// threads, this only counts errors emitted by the calling thread. It is
/// meant for code that takes a before/after delta to detect whether a unit
/// of work emitted an error, and must not be misled by errors emitted on
/// other threads at the same time (#154260). In serial compilation it is
/// equivalent to taking a delta of `err_count`.
#[inline]
pub fn err_count_on_current_thread(&self) -> usize {
THREAD_ERR_COUNT.with(Cell::get)
}

/// This excludes lint errors and delayed bugs. Unless absolutely
/// necessary, prefer `has_errors` to this method.
pub fn has_errors_excluding_lint_errors(&self) -> Option<ErrorGuaranteed> {
Expand Down Expand Up @@ -1333,6 +1359,12 @@ impl DiagCtxtInner {
}

if is_error {
// Mirror the bump of the shared error count (`err_guars` /
// `lint_err_guars` below) on a per-thread counter, so a single
// thread can tell whether *it* emitted an error without being
// affected by other threads (see `err_count_on_current_thread`).
THREAD_ERR_COUNT.with(|c| c.set(c.get() + 1));

// If we have any delayed bugs recorded, we can discard them
// because they won't be used. (This should only occur if there
// have been no errors previously emitted, because we don't add
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,14 @@ fn collect_items_rec<'tcx>(
// error count. If it has changed, a PME occurred, and we trigger some diagnostics about the
// current step of mono items collection.
//
// We use the per-thread error count rather than the global one: the mono item graph is walked
// in parallel (see the `par_for_each_in` in `collect_crate_mono_items`), so the global count
// can be bumped by an unrelated error emitted on another thread between the two reads, which
// would then wrongly blame this item (#154260). The per-thread count is not affected by errors
// emitted on other threads, so the delta reflects work done on this thread.
//
// FIXME: don't rely on global state, instead bubble up errors. Note: this is very hard to do.
let error_count = tcx.dcx().err_count();
let error_count = tcx.dcx().err_count_on_current_thread();

// In `mentioned_items` we collect items that were mentioned in this MIR but possibly do not
// need to be monomorphized. This is done to ensure that optimizing away function calls does not
Expand Down Expand Up @@ -538,7 +544,7 @@ fn collect_items_rec<'tcx>(

// Check for PMEs and emit a diagnostic if one happened. To try to show relevant edges of the
// mono item graph.
if tcx.dcx().err_count() > error_count
if tcx.dcx().err_count_on_current_thread() > error_count
&& starting_item.node.is_generic_fn()
&& starting_item.node.is_user_defined()
{
Expand Down
1 change: 0 additions & 1 deletion tests/ui/abi/simd-abi-checks-avx.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ only-x86_64
//@ build-fail
//@ compile-flags: -C target-feature=-avx
//@ ignore-parallel-frontend post-monomorphization errors
Copy link
Copy Markdown
Contributor

@zetanumbers zetanumbers Jun 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you replace these directives with an empty line instead to reduce git diff?

View changes since the review

#![feature(portable_simd)]
#![feature(simd_ffi)]
#![allow(improper_ctypes_definitions)]
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/abi/simd-abi-checks-avx.stderr
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
error: this function call uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled in the caller
--> $DIR/simd-abi-checks-avx.rs:59:11
--> $DIR/simd-abi-checks-avx.rs:58:11
|
LL | f(g());
| ^^^ function called here
|
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)

error: this function call uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled in the caller
--> $DIR/simd-abi-checks-avx.rs:59:9
--> $DIR/simd-abi-checks-avx.rs:58:9
|
LL | f(g());
| ^^^^^^ function called here
|
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)

error: this function call uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled in the caller
--> $DIR/simd-abi-checks-avx.rs:65:14
--> $DIR/simd-abi-checks-avx.rs:64:14
|
LL | gavx(favx());
| ^^^^^^ function called here
|
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)

error: this function call uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled in the caller
--> $DIR/simd-abi-checks-avx.rs:65:9
--> $DIR/simd-abi-checks-avx.rs:64:9
|
LL | gavx(favx());
| ^^^^^^^^^^^^ function called here
|
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)

error: this function call uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled in the caller
--> $DIR/simd-abi-checks-avx.rs:75:19
--> $DIR/simd-abi-checks-avx.rs:74:19
|
LL | w(Wrapper(g()));
| ^^^ function called here
|
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)

error: this function call uses SIMD vector type `Wrapper` which (with the chosen ABI) requires the `avx` target feature, which is not enabled in the caller
--> $DIR/simd-abi-checks-avx.rs:75:9
--> $DIR/simd-abi-checks-avx.rs:74:9
|
LL | w(Wrapper(g()));
| ^^^^^^^^^^^^^^^ function called here
|
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)

error: this function call uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled in the caller
--> $DIR/simd-abi-checks-avx.rs:89:9
--> $DIR/simd-abi-checks-avx.rs:88:9
|
LL | some_extern();
| ^^^^^^^^^^^^^ function called here
|
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)

error: this function definition uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled
--> $DIR/simd-abi-checks-avx.rs:24:1
--> $DIR/simd-abi-checks-avx.rs:23:1
|
LL | unsafe extern "C" fn g() -> __m256 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function defined here
|
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)

error: this function definition uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled
--> $DIR/simd-abi-checks-avx.rs:19:1
--> $DIR/simd-abi-checks-avx.rs:18:1
|
LL | unsafe extern "C" fn f(_: __m256) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function defined here
|
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)

error: this function definition uses SIMD vector type `Wrapper` which (with the chosen ABI) requires the `avx` target feature, which is not enabled
--> $DIR/simd-abi-checks-avx.rs:14:1
--> $DIR/simd-abi-checks-avx.rs:13:1
|
LL | unsafe extern "C" fn w(_: Wrapper) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function defined here
|
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)

error: this function call uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled in the caller
--> $DIR/simd-abi-checks-avx.rs:53:8
--> $DIR/simd-abi-checks-avx.rs:52:8
|
LL | || g()
| ^^^ function called here
|
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)

note: the above error was encountered while instantiating `fn in_closure::{closure#0}`
--> $DIR/simd-abi-checks-avx.rs:81:9
--> $DIR/simd-abi-checks-avx.rs:80:9
|
LL | in_closure()();
| ^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@ build-fail
//@ ignore-parallel-frontend post-monomorphization errors
// Regression test for #66975
#![warn(unconditional_panic)]
#![feature(never_type)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
error[E0080]: index out of bounds: the length is 0 but the index is 0
--> $DIR/index-out-of-bounds-never-type.rs:10:61
--> $DIR/index-out-of-bounds-never-type.rs:9:61
|
LL | const VOID: ! = { let x = 0 * std::mem::size_of::<T>(); [][x] };
| ^^^^^ evaluation of `PrintName::<()>::VOID` failed here

note: erroneous constant encountered
--> $DIR/index-out-of-bounds-never-type.rs:16:13
--> $DIR/index-out-of-bounds-never-type.rs:15:13
|
LL | let _ = PrintName::<T>::VOID;
| ^^^^^^^^^^^^^^^^^^^^

note: the above error was encountered while instantiating `fn f::<()>`
--> $DIR/index-out-of-bounds-never-type.rs:20:5
--> $DIR/index-out-of-bounds-never-type.rs:19:5
|
LL | f::<()>();
| ^^^^^^^^^
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/consts/const-eval/issue-50814-2.mir-opt.stderr
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
error[E0080]: index out of bounds: the length is 3 but the index is 42
--> $DIR/issue-50814-2.rs:17:24
--> $DIR/issue-50814-2.rs:16:24
|
LL | const BAR: usize = [5, 6, 7][T::BOO];
| ^^^^^^^^^^^^^^^^^ evaluation of `<A<()> as Foo<()>>::BAR` failed here

note: erroneous constant encountered
--> $DIR/issue-50814-2.rs:21:6
--> $DIR/issue-50814-2.rs:20:6
|
LL | &<A<T> as Foo<T>>::BAR
| ^^^^^^^^^^^^^^^^^^^^^

note: erroneous constant encountered
--> $DIR/issue-50814-2.rs:21:5
--> $DIR/issue-50814-2.rs:20:5
|
LL | &<A<T> as Foo<T>>::BAR
| ^^^^^^^^^^^^^^^^^^^^^^

note: erroneous constant encountered
--> $DIR/issue-50814-2.rs:21:5
--> $DIR/issue-50814-2.rs:20:5
|
LL | &<A<T> as Foo<T>>::BAR
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

note: erroneous constant encountered
--> $DIR/issue-50814-2.rs:21:5
--> $DIR/issue-50814-2.rs:20:5
|
LL | &<A<T> as Foo<T>>::BAR
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

note: erroneous constant encountered
--> $DIR/issue-50814-2.rs:21:5
--> $DIR/issue-50814-2.rs:20:5
|
LL | &<A<T> as Foo<T>>::BAR
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

note: erroneous constant encountered
--> $DIR/issue-50814-2.rs:21:6
--> $DIR/issue-50814-2.rs:20:6
|
LL | &<A<T> as Foo<T>>::BAR
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

note: erroneous constant encountered
--> $DIR/issue-50814-2.rs:21:6
--> $DIR/issue-50814-2.rs:20:6
|
LL | &<A<T> as Foo<T>>::BAR
| ^^^^^^^^^^^^^^^^^^^^^
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/consts/const-eval/issue-50814-2.normal.stderr
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
error[E0080]: index out of bounds: the length is 3 but the index is 42
--> $DIR/issue-50814-2.rs:17:24
--> $DIR/issue-50814-2.rs:16:24
|
LL | const BAR: usize = [5, 6, 7][T::BOO];
| ^^^^^^^^^^^^^^^^^ evaluation of `<A<()> as Foo<()>>::BAR` failed here

note: erroneous constant encountered
--> $DIR/issue-50814-2.rs:21:6
--> $DIR/issue-50814-2.rs:20:6
|
LL | &<A<T> as Foo<T>>::BAR
| ^^^^^^^^^^^^^^^^^^^^^

note: erroneous constant encountered
--> $DIR/issue-50814-2.rs:21:5
--> $DIR/issue-50814-2.rs:20:5
|
LL | &<A<T> as Foo<T>>::BAR
| ^^^^^^^^^^^^^^^^^^^^^^

note: erroneous constant encountered
--> $DIR/issue-50814-2.rs:21:6
--> $DIR/issue-50814-2.rs:20:6
|
LL | &<A<T> as Foo<T>>::BAR
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

note: the above error was encountered while instantiating `fn foo::<()>`
--> $DIR/issue-50814-2.rs:33:22
--> $DIR/issue-50814-2.rs:32:22
|
LL | println!("{:x}", foo::<()>() as *const usize as usize);
| ^^^^^^^^^^^
Expand Down
1 change: 0 additions & 1 deletion tests/ui/consts/const-eval/issue-50814-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//@ revisions: normal mir-opt
//@ [mir-opt]compile-flags: -Zmir-opt-level=4
//@ dont-require-annotations: NOTE
//@ ignore-parallel-frontend post-monomorphization errors
trait C {
const BOO: usize;
}
Expand Down
1 change: 0 additions & 1 deletion tests/ui/consts/const-eval/issue-50814.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//@ build-fail
//@ dont-require-annotations: NOTE
//@ ignore-parallel-frontend post-monomorphization errors
trait Unsigned {
const MAX: u8;
}
Expand Down
14 changes: 7 additions & 7 deletions tests/ui/consts/const-eval/issue-50814.stderr
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
error[E0080]: attempt to compute `u8::MAX + u8::MAX`, which would overflow
--> $DIR/issue-50814.rs:16:21
--> $DIR/issue-50814.rs:15:21
|
LL | const MAX: u8 = A::MAX + B::MAX;
| ^^^^^^^^^^^^^^^ evaluation of `<Sum<U8, U8> as Unsigned>::MAX` failed here

note: erroneous constant encountered
--> $DIR/issue-50814.rs:22:6
--> $DIR/issue-50814.rs:21:6
|
LL | &Sum::<U8, U8>::MAX
| ^^^^^^^^^^^^^^^^^^

error[E0080]: attempt to compute `u8::MAX + u8::MAX`, which would overflow
--> $DIR/issue-50814.rs:16:21
--> $DIR/issue-50814.rs:15:21
|
LL | const MAX: u8 = A::MAX + B::MAX;
| ^^^^^^^^^^^^^^^ evaluation of `<Sum<U8, U8> as Unsigned>::MAX` failed here
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

note: erroneous constant encountered
--> $DIR/issue-50814.rs:22:6
--> $DIR/issue-50814.rs:21:6
|
LL | &Sum::<U8, U8>::MAX
| ^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

note: erroneous constant encountered
--> $DIR/issue-50814.rs:22:5
--> $DIR/issue-50814.rs:21:5
|
LL | &Sum::<U8, U8>::MAX
| ^^^^^^^^^^^^^^^^^^^

note: erroneous constant encountered
--> $DIR/issue-50814.rs:22:6
--> $DIR/issue-50814.rs:21:6
|
LL | &Sum::<U8, U8>::MAX
| ^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

note: the above error was encountered while instantiating `fn foo::<i32>`
--> $DIR/issue-50814.rs:27:5
--> $DIR/issue-50814.rs:26:5
|
LL | foo(0);
| ^^^^^^
Expand Down
1 change: 0 additions & 1 deletion tests/ui/consts/mono-reachable-invalid-const.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@ build-fail
//@ ignore-parallel-frontend post-monomorphization errors
struct Bar<const BITS: usize>;

impl<const BITS: usize> Bar<BITS> {
Expand Down
Loading
Loading