Skip to content

Commit f2aabb7

Browse files
committed
Rollup merge of #54346 - eddyb:constant-horror, r=nikomatsakis
rustc: future-proof error reporting for polymorphic constants in types. Currently, we have 3 categories of positions where a constant can be used (`const` and associated `const` can be considered "aliases" for an expression): * runtime - if the function is polymorphic, we could even just warn and emit a panic * `static` - always monomorphic, so we can error at definition site * type-system - **must** *enforce* evaluation success where it was written That last one is the tricky one, because we can't easily turn *the presence* a type with an erroring const into a runtime panic, and we'd have to do post-monomorphization errors (which we'd rather avoid). <hr/> The solution we came up with, as part of the plans for const generics, is to require successful evaluation wherever a constant shows up in a type (currently in array lengths, and values for const parameters in the future), *through* the WF system, which means that in certain situations (e.g. function signatures) we can assume evaluation *will* succeed, and require it of users (e.g. callers) instead (we've been doing this for lifetime bounds, for a long time now, and it's pretty ergonomic). So once we do sth about #43408, this example *should* work, by propagating the responsability, to callers of `foo::<X>`, of proving `std::mem::size_of::<X>()` succeeds (and those callers can do the same). ```rust pub fn foo<T>(_: [u8; std::mem::size_of::<T>()]) {} ``` But this one *shouldn't*, as there is nothing in the signature/bounds to indicate it: ```rust pub fn bar<T>() { let _: [u8; std::mem::size_of::<T>()]; } ``` <hr/> I've come across some bit of code in the compiler that ignores const-evaluation errors *even when* they come from a constant in a type, and I've added an ICE *only when* there are no other reported errors (e.g. it's fine to ignore evaluation errors if the constant doesn't even type-check). r? @nikomatsakis cc @oli-obk @RalfJung @Centril
2 parents f5b8c7f + 046482e commit f2aabb7

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/librustc/traits/error_reporting.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
839839
"could not evaluate constant expression",
840840
) {
841841
Some(err) => err,
842-
None => return,
842+
None => {
843+
self.tcx.sess.delay_span_bug(span,
844+
&format!("constant in type had an ignored error: {:?}", err));
845+
return;
846+
}
843847
}
844848
}
845849

0 commit comments

Comments
 (0)