1
Fork 0

Rollup merge of #136884 - compiler-errors:fn-zst, r=BoxyUwU

Lower fn items as ZST valtrees and delay a bug

Lower it as a ZST instead of a const error, which we can handle mostly fine. Delay a bug so we don't accidentally support it tho.

r? BoxyUwU

Fixes #136855
Fixes #136853
Fixes #136854
Fixes #136337

Only added one test bc that's really the crux of the issue (fn item in array length position).
This commit is contained in:
Matthias Krüger 2025-02-12 06:07:39 +01:00 committed by GitHub
commit febb367a04
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 50 additions and 9 deletions

View file

@ -2154,11 +2154,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
span_bug!(span, "use of bare `static` ConstArgKind::Path's not yet supported")
}
// FIXME(const_generics): create real const to allow fn items as const paths
Res::Def(DefKind::Fn | DefKind::AssocFn, _) => ty::Const::new_error_with_message(
tcx,
span,
"fn items cannot be used as const args",
),
Res::Def(DefKind::Fn | DefKind::AssocFn, did) => {
self.dcx().span_delayed_bug(span, "function items cannot be used as const args");
let args = self.lower_generic_args_of_path_segment(
span,
did,
path.segments.last().unwrap(),
);
ty::Const::new_value(tcx, ty::ValTree::zst(), Ty::new_fn_def(tcx, did, args))
}
// Exhaustive match to be clear about what exactly we're considering to be
// an invalid Res for a const path.

View file

@ -1800,7 +1800,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
}
let u8_type = self.tcx().types.u8;
match (cv.valtree, cv.ty.kind()) {
match (cv.valtree, *cv.ty.kind()) {
(ty::ValTree::Branch(_), ty::Ref(_, inner_ty, _)) => match inner_ty.kind() {
ty::Slice(t) if *t == u8_type => {
let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
@ -1820,13 +1820,13 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
return Ok(());
}
_ => {
let cv = ty::Value { valtree: cv.valtree, ty: *inner_ty };
let cv = ty::Value { valtree: cv.valtree, ty: inner_ty };
p!("&");
p!(pretty_print_const_valtree(cv, print_ty));
return Ok(());
}
},
(ty::ValTree::Branch(_), ty::Array(t, _)) if *t == u8_type => {
(ty::ValTree::Branch(_), ty::Array(t, _)) if t == u8_type => {
let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
bug!("expected to convert valtree to raw bytes for type {:?}", t)
});
@ -1893,11 +1893,16 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
}
(ty::ValTree::Leaf(leaf), ty::Ref(_, inner_ty, _)) => {
p!(write("&"));
return self.pretty_print_const_scalar_int(leaf, *inner_ty, print_ty);
return self.pretty_print_const_scalar_int(leaf, inner_ty, print_ty);
}
(ty::ValTree::Leaf(leaf), _) => {
return self.pretty_print_const_scalar_int(leaf, cv.ty, print_ty);
}
(_, ty::FnDef(def_id, args)) => {
// Never allowed today, but we still encounter them in invalid const args.
p!(print_value_path(def_id, args));
return Ok(());
}
// FIXME(oli-obk): also pretty print arrays and other aggregate constants by reading
// their fields instead of just dumping the memory.
_ => {}

View file

@ -0,0 +1,13 @@
// Make sure we don't ICE when encountering an fn item during lowering in mGCA.
#![feature(min_generic_const_args)]
//~^ WARN the feature `min_generic_const_args` is incomplete
trait A<T> {}
impl A<[usize; fn_item]> for () {}
//~^ ERROR the constant `fn_item` is not of type `usize`
fn fn_item() {}
fn main() {}

View file

@ -0,0 +1,19 @@
warning: the feature `min_generic_const_args` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/unexpected-fn-item-in-array.rs:3:12
|
LL | #![feature(min_generic_const_args)]
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #132980 <https://github.com/rust-lang/rust/issues/132980> for more information
= note: `#[warn(incomplete_features)]` on by default
error: the constant `fn_item` is not of type `usize`
--> $DIR/unexpected-fn-item-in-array.rs:8:6
|
LL | impl A<[usize; fn_item]> for () {}
| ^^^^^^^^^^^^^^^^^^^ expected `usize`, found fn item
|
= note: the length of array `[usize; fn_item]` must be type `usize`
error: aborting due to 1 previous error; 1 warning emitted