Skip to content

Commit 2d98b20

Browse files
authored
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 Only added one test bc that's really the crux of the issue (fn item in array length position).
2 parents 926cc18 + f0cb746 commit 2d98b20

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -2154,11 +2154,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21542154
span_bug!(span, "use of bare `static` ConstArgKind::Path's not yet supported")
21552155
}
21562156
// FIXME(const_generics): create real const to allow fn items as const paths
2157-
Res::Def(DefKind::Fn | DefKind::AssocFn, _) => ty::Const::new_error_with_message(
2158-
tcx,
2159-
span,
2160-
"fn items cannot be used as const args",
2161-
),
2157+
Res::Def(DefKind::Fn | DefKind::AssocFn, did) => {
2158+
self.dcx().span_delayed_bug(span, "function items cannot be used as const args");
2159+
let args = self.lower_generic_args_of_path_segment(
2160+
span,
2161+
did,
2162+
path.segments.last().unwrap(),
2163+
);
2164+
ty::Const::new_value(tcx, ty::ValTree::zst(), Ty::new_fn_def(tcx, did, args))
2165+
}
21622166

21632167
// Exhaustive match to be clear about what exactly we're considering to be
21642168
// an invalid Res for a const path.

compiler/rustc_middle/src/ty/print/pretty.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
18001800
}
18011801

18021802
let u8_type = self.tcx().types.u8;
1803-
match (cv.valtree, cv.ty.kind()) {
1803+
match (cv.valtree, *cv.ty.kind()) {
18041804
(ty::ValTree::Branch(_), ty::Ref(_, inner_ty, _)) => match inner_ty.kind() {
18051805
ty::Slice(t) if *t == u8_type => {
18061806
let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
@@ -1820,13 +1820,13 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
18201820
return Ok(());
18211821
}
18221822
_ => {
1823-
let cv = ty::Value { valtree: cv.valtree, ty: *inner_ty };
1823+
let cv = ty::Value { valtree: cv.valtree, ty: inner_ty };
18241824
p!("&");
18251825
p!(pretty_print_const_valtree(cv, print_ty));
18261826
return Ok(());
18271827
}
18281828
},
1829-
(ty::ValTree::Branch(_), ty::Array(t, _)) if *t == u8_type => {
1829+
(ty::ValTree::Branch(_), ty::Array(t, _)) if t == u8_type => {
18301830
let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
18311831
bug!("expected to convert valtree to raw bytes for type {:?}", t)
18321832
});
@@ -1893,11 +1893,16 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
18931893
}
18941894
(ty::ValTree::Leaf(leaf), ty::Ref(_, inner_ty, _)) => {
18951895
p!(write("&"));
1896-
return self.pretty_print_const_scalar_int(leaf, *inner_ty, print_ty);
1896+
return self.pretty_print_const_scalar_int(leaf, inner_ty, print_ty);
18971897
}
18981898
(ty::ValTree::Leaf(leaf), _) => {
18991899
return self.pretty_print_const_scalar_int(leaf, cv.ty, print_ty);
19001900
}
1901+
(_, ty::FnDef(def_id, args)) => {
1902+
// Never allowed today, but we still encounter them in invalid const args.
1903+
p!(print_value_path(def_id, args));
1904+
return Ok(());
1905+
}
19011906
// FIXME(oli-obk): also pretty print arrays and other aggregate constants by reading
19021907
// their fields instead of just dumping the memory.
19031908
_ => {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Make sure we don't ICE when encountering an fn item during lowering in mGCA.
2+
3+
#![feature(min_generic_const_args)]
4+
//~^ WARN the feature `min_generic_const_args` is incomplete
5+
6+
trait A<T> {}
7+
8+
impl A<[usize; fn_item]> for () {}
9+
//~^ ERROR the constant `fn_item` is not of type `usize`
10+
11+
fn fn_item() {}
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
warning: the feature `min_generic_const_args` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/unexpected-fn-item-in-array.rs:3:12
3+
|
4+
LL | #![feature(min_generic_const_args)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #132980 <https://github.com/rust-lang/rust/issues/132980> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error: the constant `fn_item` is not of type `usize`
11+
--> $DIR/unexpected-fn-item-in-array.rs:8:6
12+
|
13+
LL | impl A<[usize; fn_item]> for () {}
14+
| ^^^^^^^^^^^^^^^^^^^ expected `usize`, found fn item
15+
|
16+
= note: the length of array `[usize; fn_item]` must be type `usize`
17+
18+
error: aborting due to 1 previous error; 1 warning emitted
19+

0 commit comments

Comments
 (0)