Skip to content

minicore: use core's diagnostic::on_unimplemented messages #143329

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use crate::fmt::Debug;
use crate::hash::{Hash, Hasher};
use crate::pin::UnsafePinned;

// NOTE: for consistent error messages between `core` and `minicore`, all `diagnostic` attributes
// should be replicated exactly in `minicore` (if `minicore` defines the item).

/// Implements a given marker trait for multiple types at the same time.
///
/// The basic syntax looks like this:
Expand Down
6 changes: 6 additions & 0 deletions src/doc/rustc-dev-guide/src/tests/minicore.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ If you find a `core` item to be missing from the [`minicore`] stub, consider
adding it to the test auxiliary if it's likely to be used or is already needed
by more than one test.

## Staying in sync with `core`

The `minicore` items must be kept up to date with `core`. For consistent
diagnostic output between using `core` and `minicore`, any `diagnostic`
attributes (e.g. `on_unimplemented`) should be replicated exactly in `minicore`.

## Example codegen test that uses `minicore`

```rust,no_run
Expand Down
20 changes: 19 additions & 1 deletion tests/auxiliary/minicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
//! # Important notes
//!
//! - `minicore` is **only** intended for `core` items, and the stubs should match the actual `core`
//! items.
//! items. For identical error output, any `diagnostic` attributes (e.g. `on_unimplemented`)
//! should also be replicated here.
//! - Be careful of adding new features and things that are only available for a subset of targets.
//!
//! # References
Expand Down Expand Up @@ -41,12 +42,24 @@ macro_rules! impl_marker_trait {
}

#[lang = "pointee_sized"]
#[diagnostic::on_unimplemented(
message = "values of type `{Self}` may or may not have a size",
label = "may or may not have a known size"
)]
Comment on lines +45 to +48
Copy link
Member

@jieyouxu jieyouxu Jul 2, 2025

Choose a reason for hiding this comment

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

Discussion [DIAG 1/2]: hm, this is kinda annoying because then you'd have to keep the diagnostic attribute content in sync, but I'm not sure of a better solution so this seems reasonable.

Comment on lines +45 to +48
Copy link
Member

@jieyouxu jieyouxu Jul 2, 2025

Choose a reason for hiding this comment

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

Suggestion [DIAG 2/2]: if we decide to go with this, can you make sure to leave a comment mentioning needing to sync the diagnostics attribute in the real core?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a note at the top of minicore.rs, or did you mean to add the note in library/core?

Copy link
Member

Choose a reason for hiding this comment

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

To make this easier to discover, can you also add this note additionally to:

  1. The real library/core and say something like "please keep the #[diagnostic::on_unimplemented] attributes in sync with tests/auxiliary/minicore.rs
  2. And also add a quick section about the sync requirement to rustc-dev-guide's minicore page https://rustc-dev-guide.rust-lang.org/tests/minicore.html
Copy link
Contributor Author

Choose a reason for hiding this comment

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

library/core/lib.rs didn't really have a place where that comment felt right (and would be seen), so I added a note to library/core/src/marker.rs, where the diagnostics we now replicate actually live.

pub trait PointeeSized {}

#[lang = "meta_sized"]
#[diagnostic::on_unimplemented(
message = "the size for values of type `{Self}` cannot be known",
label = "doesn't have a known size"
)]
pub trait MetaSized: PointeeSized {}

#[lang = "sized"]
#[diagnostic::on_unimplemented(
message = "the size for values of type `{Self}` cannot be known at compilation time",
label = "doesn't have a size known at compile-time"
)]
pub trait Sized: MetaSized {}

#[lang = "legacy_receiver"]
Expand All @@ -64,6 +77,10 @@ pub trait BikeshedGuaranteedNoDrop {}
pub unsafe auto trait Freeze {}

#[lang = "unpin"]
#[diagnostic::on_unimplemented(
note = "consider using the `pin!` macro\nconsider using `Box::pin` if you need to access the pinned value outside of the current scope",
message = "`{Self}` cannot be unpinned"
)]
pub auto trait Unpin {}

impl_marker_trait!(
Expand Down Expand Up @@ -110,6 +127,7 @@ pub struct UnsafeCell<T: PointeeSized> {
impl<T: PointeeSized> !Freeze for UnsafeCell<T> {}

#[lang = "tuple_trait"]
#[diagnostic::on_unimplemented(message = "`{Self}` is not a tuple")]
pub trait Tuple {}

#[rustc_builtin_macro]
Expand Down
16 changes: 13 additions & 3 deletions tests/ui/abi/debug.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ add-core-stubs
//@ normalize-stderr: "(abi|pref|unadjusted_abi_align): Align\([1-8] bytes\)" -> "$1: $$SOME_ALIGN"
//@ normalize-stderr: "randomization_seed: \d+" -> "randomization_seed: $$SEED"
//@ normalize-stderr: "(size): Size\([48] bytes\)" -> "$1: $$SOME_SIZE"
Expand All @@ -9,17 +10,26 @@
//@ compile-flags: -O
#![feature(rustc_attrs)]
#![crate_type = "lib"]
#![feature(no_core)]
#![no_std]
#![no_core]

extern crate minicore;
use minicore::*;

struct S(u16);

#[rustc_abi(debug)]
fn test(_x: u8) -> bool { true } //~ ERROR: fn_abi
fn test(_x: u8) -> bool {
//~^ ERROR: fn_abi
true
}

#[rustc_abi(debug)]
type TestFnPtr = fn(bool) -> u8; //~ ERROR: fn_abi

#[rustc_abi(debug)]
fn test_generic<T>(_x: *const T) { } //~ ERROR: fn_abi
fn test_generic<T>(_x: *const T) {} //~ ERROR: fn_abi

#[rustc_abi(debug)]
const C: () = (); //~ ERROR: can only be applied to
Expand All @@ -31,7 +41,7 @@ impl S {

impl S {
#[rustc_abi(debug)]
fn assoc_test(&self) { } //~ ERROR: fn_abi
fn assoc_test(&self) {} //~ ERROR: fn_abi
}

#[rustc_abi(assert_eq)]
Expand Down
30 changes: 15 additions & 15 deletions tests/ui/abi/debug.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ error: fn_abi_of(test) = FnAbi {
conv: Rust,
can_unwind: $SOME_BOOL,
}
--> $DIR/debug.rs:16:1
--> $DIR/debug.rs:23:1
|
LL | fn test(_x: u8) -> bool { true }
LL | fn test(_x: u8) -> bool {
| ^^^^^^^^^^^^^^^^^^^^^^^

error: fn_abi_of(TestFnPtr) = FnAbi {
Expand Down Expand Up @@ -185,7 +185,7 @@ error: fn_abi_of(TestFnPtr) = FnAbi {
conv: Rust,
can_unwind: $SOME_BOOL,
}
--> $DIR/debug.rs:19:1
--> $DIR/debug.rs:29:1
|
LL | type TestFnPtr = fn(bool) -> u8;
| ^^^^^^^^^^^^^^
Expand Down Expand Up @@ -263,13 +263,13 @@ error: fn_abi_of(test_generic) = FnAbi {
conv: Rust,
can_unwind: $SOME_BOOL,
}
--> $DIR/debug.rs:22:1
--> $DIR/debug.rs:32:1
|
LL | fn test_generic<T>(_x: *const T) { }
LL | fn test_generic<T>(_x: *const T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions
--> $DIR/debug.rs:25:1
--> $DIR/debug.rs:35:1
|
LL | const C: () = ();
| ^^^^^^^^^^^
Expand Down Expand Up @@ -419,7 +419,7 @@ error: ABIs are not compatible
conv: Rust,
can_unwind: $SOME_BOOL,
}
--> $DIR/debug.rs:41:1
--> $DIR/debug.rs:51:1
|
LL | type TestAbiNe = (fn(u8), fn(u32));
| ^^^^^^^^^^^^^^
Expand Down Expand Up @@ -571,7 +571,7 @@ error: ABIs are not compatible
conv: Rust,
can_unwind: $SOME_BOOL,
}
--> $DIR/debug.rs:44:1
--> $DIR/debug.rs:54:1
|
LL | type TestAbiNeLarger = (fn([u8; 32]), fn([u32; 32]));
| ^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -720,7 +720,7 @@ error: ABIs are not compatible
conv: Rust,
can_unwind: $SOME_BOOL,
}
--> $DIR/debug.rs:47:1
--> $DIR/debug.rs:57:1
|
LL | type TestAbiNeFloat = (fn(f32), fn(u32));
| ^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -870,13 +870,13 @@ error: ABIs are not compatible
conv: Rust,
can_unwind: $SOME_BOOL,
}
--> $DIR/debug.rs:51:1
--> $DIR/debug.rs:61:1
|
LL | type TestAbiNeSign = (fn(i32), fn(u32));
| ^^^^^^^^^^^^^^^^^^

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/debug.rs:54:46
--> $DIR/debug.rs:64:46
|
LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str)));
| ^^^^^^^^^^ doesn't have a size known at compile-time
Expand All @@ -885,13 +885,13 @@ LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str)));
= note: only the last element of a tuple may have a dynamically sized type

error: unrecognized argument
--> $DIR/debug.rs:56:13
--> $DIR/debug.rs:66:13
|
LL | #[rustc_abi("assert_eq")]
| ^^^^^^^^^^^

error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions
--> $DIR/debug.rs:29:5
--> $DIR/debug.rs:39:5
|
LL | const C: () = ();
| ^^^^^^^^^^^
Expand Down Expand Up @@ -981,9 +981,9 @@ error: fn_abi_of(assoc_test) = FnAbi {
conv: Rust,
can_unwind: $SOME_BOOL,
}
--> $DIR/debug.rs:34:5
--> $DIR/debug.rs:44:5
|
LL | fn assoc_test(&self) { }
LL | fn assoc_test(&self) {}
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 12 previous errors
Expand Down
13 changes: 11 additions & 2 deletions tests/ui/abi/rust-cold-works-with-rustic-args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
//@build-pass
//@compile-flags: -Clink-dead-code=true --crate-type lib
//@ add-core-stubs
//@ build-pass
//@ compile-flags: -Clink-dead-code=true
// We used to not handle all "rustic" ABIs in a (relatively) uniform way,
// so we failed to fix up arguments for actually passing through the ABI...
#![feature(rust_cold_cc)]
#![crate_type = "lib"]
#![feature(no_core)]
#![no_std]
#![no_core]

extern crate minicore;
use minicore::*;

pub extern "rust-cold" fn foo(_: [usize; 3]) {}
Loading