Skip to content
8 changes: 8 additions & 0 deletions crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,14 @@ pub enum VariantId {
impl_from!(EnumVariantId, StructId, UnionId for VariantId);

impl VariantId {
pub fn from_non_enum(adt_id: AdtId) -> Option<Self> {
Some(match adt_id {
AdtId::StructId(struct_id) => struct_id.into(),
AdtId::UnionId(union_id) => union_id.into(),
AdtId::EnumId(_) => return None,
})
}

pub fn fields(self, db: &dyn DefDatabase) -> &VariantFields {
VariantFields::of(db, self)
}
Expand Down
8 changes: 3 additions & 5 deletions crates/hir-ty/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,17 +418,15 @@ pub fn layout_of_ty_query(
.iter()
.map(|pat| match pat.kind() {
PatternKind::Range { start, end } => Ok::<_, LayoutError>((
extract_const_value(start)
.unwrap()
extract_const_value(start)?
.try_to_bits(db, trait_env.as_ref())
.ok_or(LayoutError::Unknown)?,
extract_const_value(end)
.unwrap()
extract_const_value(end)?
.try_to_bits(db, trait_env.as_ref())
.ok_or(LayoutError::Unknown)?,
)),
PatternKind::NotNull | PatternKind::Or(_) => {
unreachable!("mixed or patterns are not allowed")
Err(LayoutError::Unknown)
}
})
.collect();
Expand Down
14 changes: 11 additions & 3 deletions crates/hir-ty/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,21 @@ fn non_zero_and_non_null() {
}
check_size_and_align(
r#"
const END: usize = 10;
struct Goal(core::pattern_type!(usize is 0..=END));
"#,
const END: usize = 10;
struct Goal(core::pattern_type!(usize is 0..=END));
"#,
"//- minicore: pat\n",
8,
8,
);
check_size_and_align(
r#"
pub struct Goal(core::pattern_type!(i32 is ..0 | 1..));
"#,
"//- minicore: pat\n",
4,
4,
);
}

#[test]
Expand Down
23 changes: 16 additions & 7 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use thin_vec::ThinVec;
use tracing::debug;

use crate::{
ImplTraitId, Span, TyLoweringDiagnostic, TyLoweringDiagnosticKind,
ImplTraitId, Span, TyLoweringDiagnostic,
consteval::{create_anon_const, path_to_const},
db::{AnonConstId, GeneralConstId, HirDatabase, InternedOpaqueTyId},
generics::{Generics, SingleGenerics, generics},
Expand Down Expand Up @@ -302,8 +302,14 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
self
}

pub(crate) fn push_diagnostic(&mut self, type_ref: TypeRefId, kind: TyLoweringDiagnosticKind) {
self.diagnostics.push(TyLoweringDiagnostic { source: type_ref, kind });
pub(crate) fn push_diagnostic(&mut self, diagnostic: TyLoweringDiagnostic) {
self.diagnostics.push(diagnostic);
}

fn push_infer_vars_not_allowed(&mut self, span: Span) {
if !span.is_dummy() {
self.push_diagnostic(TyLoweringDiagnostic::InferVarsNotAllowed { source: span });
}
}

#[track_caller]
Expand All @@ -315,7 +321,7 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
match &mut self.infer_vars {
Some(infer_vars) => infer_vars.next_ty_var(span),
None => {
// FIXME: Emit an error: no infer vars allowed here.
self.push_infer_vars_not_allowed(span);
self.types.types.error
}
}
Expand All @@ -325,7 +331,7 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
match &mut self.infer_vars {
Some(infer_vars) => infer_vars.next_const_var(span),
None => {
// FIXME: Emit an error: no infer vars allowed here.
self.push_infer_vars_not_allowed(span);
self.types.consts.error
}
}
Expand All @@ -335,7 +341,7 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
match &mut self.infer_vars {
Some(infer_vars) => infer_vars.next_region_var(span),
None => {
// FIXME: Emit an error: no infer vars allowed here.
self.push_infer_vars_not_allowed(span);
self.types.regions.error
}
}
Expand Down Expand Up @@ -634,7 +640,10 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
data: Either::Left(PathDiagnosticCallbackData(type_ref)),
callback: |data, this, diag| {
let type_ref = data.as_ref().left().unwrap().0;
this.push_diagnostic(type_ref, TyLoweringDiagnosticKind::PathDiagnostic(diag))
this.push_diagnostic(TyLoweringDiagnostic::PathDiagnostic {
source: type_ref,
diag,
})
},
}
}
Expand Down
14 changes: 5 additions & 9 deletions crates/hir-ty/src/lower/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
//! This files contains the declaration of diagnostics kinds for ty and path lowering.

use hir_def::type_ref::TypeRefId;
use hir_def::{GenericDefId, GenericParamId};
use hir_def::{GenericDefId, GenericParamId, type_ref::TypeRefId};

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct TyLoweringDiagnostic {
pub source: TypeRefId,
pub kind: TyLoweringDiagnosticKind,
}
use crate::Span;

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum TyLoweringDiagnosticKind {
PathDiagnostic(PathLoweringDiagnostic),
pub enum TyLoweringDiagnostic {
PathDiagnostic { source: TypeRefId, diag: PathLoweringDiagnostic },
InferVarsNotAllowed { source: Span },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
Loading