Skip to content
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

Lower fn items as ZST valtrees and delay a bug #136884

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 9 additions & 5 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand All @@ -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)
});
Expand Down Expand Up @@ -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.
_ => {}
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/const-generics/mgca/unexpected-fn-item-in-array.rs
Original file line number Diff line number Diff line change
@@ -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() {}
19 changes: 19 additions & 0 deletions tests/ui/const-generics/mgca/unexpected-fn-item-in-array.stderr
Original file line number Diff line number Diff line change
@@ -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

Loading