Skip to content
Open
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
83 changes: 83 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3592,6 +3592,41 @@ mod enum_tests {
);
}

#[test]
fn enum_cast_reshaping_enum_free_siblings_is_ok_2() {
// This one has an enum with a left sibling which is much bigger (as a HL type DAG) in the
// source type than the target.
let result = analyze(
"enum E { A, B, }
fn main() {
let x: ((Either<(), u8>, Either<(), u8>, Either<(), u8>), E)
= ((Left(()), Left(()), Left(())), E::A);
let _y: ((Option<u8>, Option<u8>, Option<u8>), E)
= <((Either<(), u8>, Either<(), u8>, Either<(), u8>), E)>::into(x);
}",
);
assert!(
result.is_ok(),
"reshaping enum-free siblings must stay castable: {result:?}"
);

// Same thing, but we try to swap out the enums. This should fail.
let result = analyze(
"enum E { A, B, }
enum F { C, D, }
fn main() {
let x: ((Either<(), u8>, Either<(), u8>, Either<(), u8>), E)
= ((Left(()), Left(()), Left(())), E::A);
let _y: ((Option<u8>, Option<u8>, Option<u8>), F)
= <((Either<(), u8>, Either<(), u8>, Either<(), u8>), E)>::into(x);
}",
);
assert!(
result.is_err(),
"reshaping enum-free siblings must stay non-castable: {result:?}"
);
}

#[test]
fn enum_cast_to_itself_is_ok() {
let result = analyze(
Expand All @@ -3607,6 +3642,54 @@ mod enum_tests {
);
}

#[test]
fn enum_cast_option_either() {
let result = analyze(
"enum E { A, B, }
fn main() {
let x: Option<E> = None;
let _y: Either<(), E> = <Option<E>>::into(x);
}",
);
result.expect_err("this should work");
}

#[test]
fn enum_cast_array_tuple() {
let result = analyze(
"enum E { A, B, }
fn main() {
let x: [E; 2] = [E::A, E::B];
let _y: (E, E) = <[E; 2]>::into(x);
}",
);
result.expect_err("this should work");
}

#[test]
fn enum_cast_list1_option() {
let result = analyze(
"enum E { A, B, }
fn main() {
let x: List<E, 2> = list![];
let _y: Option<E> = <List<E, 2>>::into(x);
}",
);
result.expect_err("this should work");
}

#[test]
fn enum_cast_list2_option() {
let result = analyze(
"enum E { A, B, }
fn main() {
let x: List<E, 4> = list![];
let _y: (Option<(E, E)>, Option<E>) = <List<E, 4>>::into(x);
}",
);
result.expect_err("this should work");
}

#[test]
fn enum_named_after_builtin_type_is_rejected() {
// `enum Signature` would shadow the built-in alias: constructions
Expand Down
32 changes: 22 additions & 10 deletions src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::array::BTreeSlice;
use crate::error::Error;
use crate::named::{CoreExt, PairBuilder, SelectorBuilder};
use crate::str::Identifier;
use crate::types::{ResolvedType, TypeInner};
use crate::types::{ResolvedType, TypeDeconstructible};
use crate::unstable::impl_require_feature;

/// Pattern for binding values to variables.
Expand Down Expand Up @@ -51,25 +51,37 @@ impl Pattern {
let mut stack = vec![(self, ty)];
let mut output = HashMap::new();
while let Some((pattern, ty)) = stack.pop() {
match (pattern, ty.as_inner()) {
(Pattern::Identifier(i), _) => match output.entry(i.clone()) {
let unexpected_err = || Err(Error::ExpressionUnexpectedType { ty: ty.clone() });
match pattern {
Pattern::Identifier(i) => match output.entry(i.clone()) {
Entry::Occupied(..) => {
return Err(Error::VariableReuseInPattern {
identifier: i.clone(),
})
});
}
Entry::Vacant(entry) => {
entry.insert(ty.clone());
}
},
(Pattern::Ignore, _) => {}
(Pattern::Tuple(pats), TypeInner::Tuple(types)) => {
stack.extend(pats.iter().zip(types.iter().map(Arc::as_ref)));
Pattern::Ignore => {}
Pattern::Tuple(pats) => {
if let Some(types) = ty.as_tuple() {
stack.extend(pats.iter().zip(types.iter().map(Arc::as_ref)));
} else {
return unexpected_err();
}
}
(Pattern::Array(pats), TypeInner::Array(ty, size)) if pats.len() == *size => {
stack.extend(pats.iter().zip(std::iter::repeat(ty.as_ref())));
Pattern::Array(pats) => {
if let Some((ty, size)) = ty.as_array() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 7b31e94 LLM finding:
this line shadows the outer ty with the element type, so the length-mismatch arm reports the wrong type:

this PR: Expected expression of type u8; found something else
master: Expected expression of type [u8; 3]; found something else
Renaming the binding to elem_ty fixes it. Minor: the commit message mentions as_list / lists, but Pattern has no List variant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced the explicit return value with an auxiliary closure which avoids the shadowing and also reduces code duplication.

Also updated the commit message.

if pats.len() == size {
stack.extend(pats.iter().zip(std::iter::repeat(ty)));
} else {
return unexpected_err();
}
} else {
return unexpected_err();
}
}
_ => return Err(Error::ExpressionUnexpectedType { ty: ty.clone() }),
}
}
Ok(output)
Expand Down
Loading
Loading