Skip to content

Commit

Permalink
Fix intrinsic const parameter counting with effects
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed Jul 7, 2024
1 parent 289deb9 commit 4f54193
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 31 deletions.
18 changes: 12 additions & 6 deletions compiler/rustc_hir_analysis/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,20 @@ 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")
.emit();
return;
}
};
let own_counts = generics.own_counts();

let gen_count_ok = |found: usize, expected: usize, descr: &str| -> bool {
if found != expected {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/intrinsics/not-overridden.rs
Original file line number Diff line number Diff line change
@@ -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" -> ""
Expand Down
52 changes: 52 additions & 0 deletions tests/ui/intrinsics/safe-intrinsic-mismatch.effects.stderr
Original file line number Diff line number Diff line change
@@ -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`.
7 changes: 6 additions & 1 deletion tests/ui/intrinsics/safe-intrinsic-mismatch.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
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;
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= 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
Expand All @@ -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
Expand Down
42 changes: 39 additions & 3 deletions tests/ui/rfcs/rfc-2632-const-trait-impl/effects/minicore.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -532,3 +536,35 @@ fn test_const_eval_select() {

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;
}
}
24 changes: 10 additions & 14 deletions tests/ui/rfcs/rfc-2632-const-trait-impl/effects/minicore.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4f54193

Please sign in to comment.