Skip to content

Check signature WF when lowering MIR body #137298

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 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 1 addition & 6 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,14 +745,10 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
match tcx.def_kind(def_id) {
DefKind::Static { .. } => {
tcx.ensure_ok().typeck(def_id);
Copy link
Member Author

Choose a reason for hiding this comment

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

These are body owners, so they'll be typeck'd like all other body owners later on.

maybe_check_static_with_link_section(tcx, def_id);
check_static_inhabited(tcx, def_id);
check_static_linkage(tcx, def_id);
}
DefKind::Const => {
tcx.ensure_ok().typeck(def_id);
}
DefKind::Const => {}
DefKind::Enum => {
check_enum(tcx, def_id);
}
Expand All @@ -766,7 +762,6 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
ExternAbi::Rust,
)
}
// Everything else is checked entirely within check_item_body
}
DefKind::Impl { of_trait } => {
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn forbid_intrinsic_abi(tcx: TyCtxt<'_>, sp: Span, abi: ExternAbi) {
}
}

fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId) {
pub(super) fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId) {
// Only restricted on wasm target for now
if !tcx.sess.target.is_like_wasm {
return;
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
tcx.par_hir_body_owners(|item_def_id| {
let def_kind = tcx.def_kind(item_def_id);
match def_kind {
DefKind::Static { .. } => tcx.ensure_ok().eval_static_initializer(item_def_id),
DefKind::Static { .. } => {
tcx.ensure_ok().eval_static_initializer(item_def_id);
check::maybe_check_static_with_link_section(tcx, item_def_id);
}
DefKind::Const if tcx.generics_of(item_def_id).is_empty() => {
let instance = ty::Instance::new(item_def_id.into(), ty::GenericArgs::empty());
let cid = GlobalId { instance, promoted: None };
Expand All @@ -223,12 +226,9 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
}
});

// FIXME: Remove this when we implement creating `DefId`s
// for anon constants during their parents' typeck.
// Typeck all body owners in parallel will produce queries
// cycle errors because it may typeck on anon constants directly.
tcx.par_hir_body_owners(|item_def_id| {
let def_kind = tcx.def_kind(item_def_id);
// Skip `AnonConst`s because we feed their `type_of`.
if !matches!(def_kind, DefKind::AnonConst) {
tcx.ensure_ok().typeck(item_def_id);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/issues/issue-79494.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

#![feature(const_transmute)]

const ZST: &[u8] = unsafe { std::mem::transmute(1usize) }; //~ ERROR cannot transmute between types of different sizes, or dependently-sized types
pub const ZST: &[u8] = unsafe { std::mem::transmute(1usize) }; //~ ERROR cannot transmute between types of different sizes, or dependently-sized types
Copy link
Contributor

Choose a reason for hiding this comment

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

why this pub?

Copy link
Member Author

Choose a reason for hiding this comment

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

so we can keep the error

6 changes: 3 additions & 3 deletions tests/rustdoc-ui/issues/issue-79494.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> $DIR/issue-79494.rs:5:29
--> $DIR/issue-79494.rs:5:33
|
LL | const ZST: &[u8] = unsafe { std::mem::transmute(1usize) };
| ^^^^^^^^^^^^^^^^^^^
LL | pub const ZST: &[u8] = unsafe { std::mem::transmute(1usize) };
| ^^^^^^^^^^^^^^^^^^^
|
= note: source type: `usize` (64 bits)
= note: target type: `&[u8]` (128 bits)
Expand Down
3 changes: 2 additions & 1 deletion tests/rustdoc-ui/track-diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

struct A;
struct B;
const S: A = B;

pub const S: A = B;
Copy link
Member Author

Choose a reason for hiding this comment

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

A side-effect of this is that consts are only required to typeck if they're being documented.

Copy link
Contributor

@lcnr lcnr Mar 4, 2025

Choose a reason for hiding this comment

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

in rustdoc

Copy link
Member Author

Choose a reason for hiding this comment

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

oh 💀 yes in roblox

4 changes: 2 additions & 2 deletions tests/rustdoc-ui/track-diagnostics.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0308]: mismatched types
--> $DIR/track-diagnostics.rs:LL:CC
|
LL | const S: A = B;
| ^ expected `A`, found `B`
LL | pub const S: A = B;
| ^ expected `A`, found `B`
-Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:LL:CC

error: aborting due to 1 previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ warning: type `v17` should have an upper camel case name
LL | pub struct v17<const v10: usize, const v7: v11> {
| ^^^ help: convert the identifier to upper camel case (notice the capitalization): `V17`

error[E0425]: cannot find function `v6` in this scope
--> $DIR/unevaluated-const-ice-119731.rs:13:35
|
LL | const v0: [[usize; v4]; v4] = v6(v8);
| ^^ not found in this scope

error: `[[usize; v4]; v4]` is forbidden as the type of a const generic parameter
--> $DIR/unevaluated-const-ice-119731.rs:16:48
|
Expand All @@ -72,6 +66,12 @@ help: add `#![feature(adt_const_params)]` to the crate attributes to enable more
LL + #![feature(adt_const_params)]
|

error[E0425]: cannot find function `v6` in this scope
--> $DIR/unevaluated-const-ice-119731.rs:13:35
|
LL | const v0: [[usize; v4]; v4] = v6(v8);
| ^^ not found in this scope

error: maximum number of nodes exceeded in constant v20::v17::<v10, v2>::{constant#0}
--> $DIR/unevaluated-const-ice-119731.rs:28:37
|
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/consts/issue-39974.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
error[E0308]: mismatched types
--> $DIR/issue-39974.rs:5:19
|
LL | f: [[f64; 2]; LENGTH],
| ^^^^^^ expected `usize`, found `f64`

error[E0308]: mismatched types
--> $DIR/issue-39974.rs:1:21
|
Expand All @@ -9,12 +15,6 @@ help: use a float literal
LL | const LENGTH: f64 = 2.0;
| ++

error[E0308]: mismatched types
--> $DIR/issue-39974.rs:5:19
|
LL | f: [[f64; 2]; LENGTH],
| ^^^^^^ expected `usize`, found `f64`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
18 changes: 9 additions & 9 deletions tests/ui/layout/issue-84108.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ LL | const BAR: (&Path, [u8], usize) = ("hello", [], 42);
= help: the trait `Sized` is not implemented for `[u8]`
= note: only the last element of a tuple may have a dynamically sized type

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/issue-84108.rs:15:13
|
LL | static BAZ: ([u8], usize) = ([], 0);
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u8]`
= note: only the last element of a tuple may have a dynamically sized type

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/issue-84108.rs:9:12
|
Expand All @@ -48,15 +57,6 @@ LL | const BAR: (&Path, [u8], usize) = ("hello", [], 42);
= note: expected slice `[u8]`
found array `[_; 0]`

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/issue-84108.rs:15:13
|
LL | static BAZ: ([u8], usize) = ([], 0);
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u8]`
= note: only the last element of a tuple may have a dynamically sized type

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/issue-84108.rs:15:13
|
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/type-alias-impl-trait/issue-53092-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#![allow(dead_code)]

type Bug<T, U> = impl Fn(T) -> U + Copy;
//~^ ERROR cycle detected

const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
//~^ ERROR cycle detected
//~| ERROR: non-defining opaque type use
//~^ ERROR item does not constrain `Bug::{opaque#0}`, but has it in its signature
//~| ERROR item does not constrain `Bug::{opaque#0}`, but has it in its signature
//~| ERROR non-defining opaque type use in defining scope

fn make_bug<T, U: From<T>>() -> Bug<T, U> {
|x| x.into()
Expand Down
50 changes: 38 additions & 12 deletions tests/ui/type-alias-impl-trait/issue-53092-2.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0792]: non-defining opaque type use in defining scope
--> $DIR/issue-53092-2.rs:6:18
--> $DIR/issue-53092-2.rs:7:18
|
LL | const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
| ^^^^^^^^^^^ argument `u8` is not a generic parameter
Expand All @@ -10,33 +10,59 @@ note: for this opaque type
LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
| ^^^^^^^^^^^^^^^^^^^^^^

error[E0391]: cycle detected when type-checking `CONST_BUG`
--> $DIR/issue-53092-2.rs:6:1
error[E0391]: cycle detected when computing type of `Bug::{opaque#0}`
--> $DIR/issue-53092-2.rs:4:18
|
LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
| ^^^^^^^^^^^^^^^^^^^^^^
|
note: ...which requires computing type of opaque `Bug::{opaque#0}`...
--> $DIR/issue-53092-2.rs:4:18
|
LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
| ^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires type-checking `CONST_BUG`...
--> $DIR/issue-53092-2.rs:7:1
|
LL | const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: ...which requires computing layout of `Bug<u8, ()>`...
= note: ...which requires normalizing `Bug<u8, ()>`...
note: ...which requires computing type of `Bug::{opaque#0}`...
= note: ...which again requires computing type of `Bug::{opaque#0}`, completing the cycle
note: cycle used when checking that `Bug::{opaque#0}` is well-formed
--> $DIR/issue-53092-2.rs:4:18
|
LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
| ^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires computing type of opaque `Bug::{opaque#0}`...
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

error: item does not constrain `Bug::{opaque#0}`, but has it in its signature
--> $DIR/issue-53092-2.rs:7:7
|
LL | const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
| ^^^^^^^^^
|
= note: consider moving the opaque type's declaration and defining uses into a separate module
note: this opaque type is in the signature
--> $DIR/issue-53092-2.rs:4:18
|
LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
| ^^^^^^^^^^^^^^^^^^^^^^
= note: ...which again requires type-checking `CONST_BUG`, completing the cycle
note: cycle used when checking that `CONST_BUG` is well-formed
--> $DIR/issue-53092-2.rs:6:1

error: item does not constrain `Bug::{opaque#0}`, but has it in its signature
--> $DIR/issue-53092-2.rs:7:61
|
LL | const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
| ^^^^^^^
|
= note: consider moving the opaque type's declaration and defining uses into a separate module
note: this opaque type is in the signature
--> $DIR/issue-53092-2.rs:4:18
|
LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
| ^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors
error: aborting due to 4 previous errors

Some errors have detailed explanations: E0391, E0792.
For more information about an error, try `rustc --explain E0391`.
20 changes: 10 additions & 10 deletions tests/ui/wf/wf-normalization-sized.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ LL | const _: <[[[[[[u8]]]]]] as WellUnformed>::RequestNormalize = ();
= help: the trait `Sized` is not implemented for `[[[[[u8]]]]]`
= note: slice and array elements must have `Sized` type

error[E0277]: the size for values of type `[[[[[u8]]]]]` cannot be known at compilation time
--> $DIR/wf-normalization-sized.rs:19:11
|
LL | const _: <[[[[[[u8]]]]]] as WellUnformed>::RequestNormalize = ();
| ^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[[[[[u8]]]]]`
= note: slice and array elements must have `Sized` type
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/wf-normalization-sized.rs:22:11
|
Expand All @@ -27,6 +17,16 @@ LL | const _: <Vec<str> as WellUnformed>::RequestNormalize = ();
note: required by an implicit `Sized` bound in `Vec`
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

error[E0277]: the size for values of type `[[[[[u8]]]]]` cannot be known at compilation time
--> $DIR/wf-normalization-sized.rs:19:11
|
LL | const _: <[[[[[[u8]]]]]] as WellUnformed>::RequestNormalize = ();
| ^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[[[[[u8]]]]]`
= note: slice and array elements must have `Sized` type
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/wf-normalization-sized.rs:22:11
|
Expand Down
Loading