Rollup merge of #127452 - fee1-dead-contrib:fx-intrinsic-counting, r=fmease

Fix intrinsic const parameter counting with `effects`

r? project-const-traits
This commit is contained in:
许杰友 Jieyou Xu (Joe) 2024-07-08 13:04:34 +08:00 committed by GitHub
commit 73593b9aca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 126 additions and 31 deletions

View File

@ -26,15 +26,12 @@ fn equate_intrinsic_type<'tcx>(
n_cts: usize,
sig: ty::PolyFnSig<'tcx>,
) {
let (own_counts, span) = match tcx.hir_node_by_def_id(def_id) {
let (generics, span) = match tcx.hir_node_by_def_id(def_id) {
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. })
| hir::Node::ForeignItem(hir::ForeignItem {
kind: hir::ForeignItemKind::Fn(.., generics, _),
..
}) => {
let own_counts = tcx.generics_of(def_id).own_counts();
(own_counts, generics.span)
}
}) => (tcx.generics_of(def_id), generics.span),
_ => {
struct_span_code_err!(tcx.dcx(), span, E0622, "intrinsic must be a function")
.with_span_label(span, "expected a function")
@ -42,6 +39,7 @@ fn equate_intrinsic_type<'tcx>(
return;
}
};
let own_counts = generics.own_counts();
let gen_count_ok = |found: usize, expected: usize, descr: &str| -> bool {
if found != expected {
@ -57,9 +55,17 @@ fn equate_intrinsic_type<'tcx>(
}
};
// the host effect param should be invisible as it shouldn't matter
// whether effects is enabled for the intrinsic provider crate.
let consts_count = if generics.host_effect_index.is_some() {
own_counts.consts - 1
} else {
own_counts.consts
};
if gen_count_ok(own_counts.lifetimes, n_lts, "lifetime")
&& gen_count_ok(own_counts.types, n_tps, "type")
&& gen_count_ok(own_counts.consts, n_cts, "const")
&& gen_count_ok(consts_count, n_cts, "const")
{
let _ = check_function_signature(
tcx,

View File

@ -1,6 +1,6 @@
//! Check that intrinsics that do not get overridden, but are marked as such,
//! cause an error instead of silently invoking the body.
#![feature(rustc_attrs/* , effects*/)] // FIXME(effects)
#![feature(rustc_attrs)]
//@ build-fail
//@ failure-status:101
//@ normalize-stderr-test ".*note: .*\n\n" -> ""

View File

@ -0,0 +1,52 @@
error: using `#![feature(effects)]` without enabling next trait solver globally
|
= note: the next trait solver must be enabled globally for the effects feature to work correctly
= help: use `-Znext-solver` to enable
error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `size_of`
--> $DIR/safe-intrinsic-mismatch.rs:11:5
|
LL | fn size_of<T>() -> usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `size_of`
--> $DIR/safe-intrinsic-mismatch.rs:11:5
|
LL | fn size_of<T>() -> usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `assume`
--> $DIR/safe-intrinsic-mismatch.rs:16:1
|
LL | const fn assume(_b: bool) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0308]: intrinsic has wrong type
--> $DIR/safe-intrinsic-mismatch.rs:16:16
|
LL | const fn assume(_b: bool) {}
| ^ expected unsafe fn, found safe fn
|
= note: expected signature `unsafe fn(_)`
found signature `fn(_)`
error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `const_deallocate`
--> $DIR/safe-intrinsic-mismatch.rs:20:1
|
LL | const fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0308]: intrinsic has wrong type
--> $DIR/safe-intrinsic-mismatch.rs:20:26
|
LL | const fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize) {}
| ^ expected unsafe fn, found safe fn
|
= note: expected signature `unsafe fn(_, _, _)`
found signature `fn(_, _, _)`
error: aborting due to 7 previous errors
For more information about this error, try `rustc --explain E0308`.

View File

@ -1,6 +1,11 @@
//@ revisions: stock effects
#![feature(intrinsics)]
#![feature(rustc_attrs)]
// FIXME(effects) do this with revisions #![feature(effects)]
// as effects insert a const generic param to const intrinsics,
// check here that it doesn't report a const param mismatch either
// enabling or disabling effects.
#![cfg_attr(effects, feature(effects))]
#![allow(incomplete_features)]
extern "rust-intrinsic" {
fn size_of<T>() -> usize; //~ ERROR intrinsic safety mismatch

View File

@ -1,11 +1,11 @@
error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `size_of`
--> $DIR/safe-intrinsic-mismatch.rs:6:5
--> $DIR/safe-intrinsic-mismatch.rs:11:5
|
LL | fn size_of<T>() -> usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `size_of`
--> $DIR/safe-intrinsic-mismatch.rs:6:5
--> $DIR/safe-intrinsic-mismatch.rs:11:5
|
LL | fn size_of<T>() -> usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^
@ -13,13 +13,13 @@ LL | fn size_of<T>() -> usize;
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `assume`
--> $DIR/safe-intrinsic-mismatch.rs:11:1
--> $DIR/safe-intrinsic-mismatch.rs:16:1
|
LL | const fn assume(_b: bool) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0308]: intrinsic has wrong type
--> $DIR/safe-intrinsic-mismatch.rs:11:16
--> $DIR/safe-intrinsic-mismatch.rs:16:16
|
LL | const fn assume(_b: bool) {}
| ^ expected unsafe fn, found safe fn
@ -28,13 +28,13 @@ LL | const fn assume(_b: bool) {}
found signature `fn(_)`
error: intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `const_deallocate`
--> $DIR/safe-intrinsic-mismatch.rs:15:1
--> $DIR/safe-intrinsic-mismatch.rs:20:1
|
LL | const fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0308]: intrinsic has wrong type
--> $DIR/safe-intrinsic-mismatch.rs:15:26
--> $DIR/safe-intrinsic-mismatch.rs:20:26
|
LL | const fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize) {}
| ^ expected unsafe fn, found safe fn

View File

@ -1,12 +1,16 @@
//@ known-bug: #110395
//@ failure-status: 101
//@ normalize-stderr-test ".*note: .*\n\n" -> ""
//@ normalize-stderr-test "thread 'rustc' panicked.*:\n.*\n" -> ""
//@ rustc-env:RUST_BACKTRACE=0
// FIXME(effects) check-pass
// FIXME(effects) fix intrinsics const parameter counting
//@ compile-flags: -Znext-solver
#![crate_type = "lib"]
#![feature(no_core, lang_items, unboxed_closures, auto_traits, intrinsics, rustc_attrs, staged_api)]
#![feature(fundamental)]
#![feature(fundamental, marker_trait_attr)]
#![feature(const_trait_impl, effects, const_mut_refs)]
#![allow(internal_features)]
#![allow(internal_features, incomplete_features)]
#![no_std]
#![no_core]
#![stable(feature = "minicore", since = "1.0.0")]
@ -532,3 +536,35 @@ fn rt_fn() {}
const_eval_select((), const_fn, rt_fn);
}
mod effects {
use super::Sized;
#[lang = "EffectsNoRuntime"]
pub struct NoRuntime;
#[lang = "EffectsMaybe"]
pub struct Maybe;
#[lang = "EffectsRuntime"]
pub struct Runtime;
#[lang = "EffectsCompat"]
pub trait Compat<#[rustc_runtime] const RUNTIME: bool> {}
impl Compat<false> for NoRuntime {}
impl Compat<true> for Runtime {}
impl<#[rustc_runtime] const RUNTIME: bool> Compat<RUNTIME> for Maybe {}
#[lang = "EffectsTyCompat"]
#[marker]
pub trait TyCompat<T: ?Sized> {}
impl<T: ?Sized> TyCompat<T> for T {}
impl<T: ?Sized> TyCompat<T> for Maybe {}
impl<T: ?Sized> TyCompat<Maybe> for T {}
#[lang = "EffectsIntersection"]
pub trait Intersection {
#[lang = "EffectsIntersectionOutput"]
type Output: ?Sized;
}
}

View File

@ -1,17 +1,13 @@
warning: the feature `effects` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/minicore.rs:8:30
|
LL | #![feature(const_trait_impl, effects, const_mut_refs)]
| ^^^^^^^
|
= note: see issue #102090 <https://github.com/rust-lang/rust/issues/102090> for more information
= note: `#[warn(incomplete_features)]` on by default
error: the compiler unexpectedly panicked. this is a bug.
error: requires `EffectsCompat` lang_item
--> $DIR/minicore.rs:455:9
|
LL | impl<T: Clone> Clone for RefCell<T> {
| ^^^^^
query stack during panic:
#0 [check_well_formed] checking that `<impl at $DIR/minicore.rs:459:1: 459:36>` is well-formed
#1 [check_mod_type_wf] checking that types are well-formed in top-level module
end of query stack
error: aborting due to 1 previous error; 1 warning emitted
error: the compiler unexpectedly panicked. this is a bug.
query stack during panic:
#0 [check_well_formed] checking that `drop` is well-formed
#1 [check_mod_type_wf] checking that types are well-formed in top-level module
end of query stack