From 431dd32f8a57436946123f7ff83f2070c6cba192 Mon Sep 17 00:00:00 2001 From: hkalbasi Date: Sun, 14 May 2023 21:05:33 +0330 Subject: [PATCH] Unsized temporary is not an implementation error --- crates/hir-ty/src/consteval/tests.rs | 19 +++++++++++++++++++ crates/hir-ty/src/mir/lower.rs | 4 +++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/hir-ty/src/consteval/tests.rs b/crates/hir-ty/src/consteval/tests.rs index 12b15065ddf..2aac1cc057d 100644 --- a/crates/hir-ty/src/consteval/tests.rs +++ b/crates/hir-ty/src/consteval/tests.rs @@ -2066,3 +2066,22 @@ fn type_error() { |e| matches!(e, ConstEvalError::MirLowerError(MirLowerError::TypeMismatch(_))), ); } + +#[test] +fn unsized_local() { + check_fail( + r#" + //- minicore: coerce_unsized, index, slice + const fn x() -> SomeUnknownTypeThatDereferenceToSlice { + SomeUnknownTypeThatDereferenceToSlice + } + + const GOAL: u16 = { + let y = x(); + let z: &[u16] = &y; + z[1] + }; + "#, + |e| matches!(e, ConstEvalError::MirLowerError(MirLowerError::UnsizedTemporary(_))), + ); +} diff --git a/crates/hir-ty/src/mir/lower.rs b/crates/hir-ty/src/mir/lower.rs index 66b1d840bdc..627c36dca92 100644 --- a/crates/hir-ty/src/mir/lower.rs +++ b/crates/hir-ty/src/mir/lower.rs @@ -75,6 +75,7 @@ pub enum MirLowerError { RecordLiteralWithoutPath, UnresolvedMethod(String), UnresolvedField, + UnsizedTemporary(Ty), MissingFunctionDefinition, TypeMismatch(TypeMismatch), /// This should be never happen. Type mismatch should catch everything. @@ -108,6 +109,7 @@ pub fn pretty_print( } } MirLowerError::LayoutError(_) + | MirLowerError::UnsizedTemporary(_) | MirLowerError::IncompleteExpr | MirLowerError::UnaccessableLocal | MirLowerError::TraitFunctionDefinition(_, _) @@ -199,7 +201,7 @@ fn new( fn temp(&mut self, ty: Ty) -> Result { if matches!(ty.kind(Interner), TyKind::Slice(_) | TyKind::Dyn(_)) { - implementation_error!("unsized temporaries"); + return Err(MirLowerError::UnsizedTemporary(ty)); } Ok(self.result.locals.alloc(Local { ty })) }