Merge pull request #18607 from ChayimFriedman2/pattern-record-no-record

fix: Fix shadowing of record enum variant in patterns
This commit is contained in:
Laurențiu Nicola 2024-12-04 03:51:41 +00:00 committed by GitHub
commit 273d481922
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 7 deletions

View file

@ -1510,20 +1510,20 @@ impl ExprCollector<'_> {
BuiltinShadowMode::Other,
None,
);
// Funnily enough, record structs/variants *can* be shadowed
// by pattern bindings (but unit or tuple structs/variants
// can't).
match resolved.take_values() {
Some(ModuleDefId::ConstId(_)) => (None, Pat::Path(name.into())),
Some(ModuleDefId::EnumVariantId(_)) => {
// this is only really valid for unit variants, but
// shadowing other enum variants with a pattern is
// an error anyway
Some(ModuleDefId::EnumVariantId(variant))
if self.db.variant_data(variant.into()).kind()
!= StructKind::Record =>
{
(None, Pat::Path(name.into()))
}
Some(ModuleDefId::AdtId(AdtId::StructId(s)))
if self.db.struct_data(s).variant_data.kind() != StructKind::Record =>
{
// Funnily enough, record structs *can* be shadowed
// by pattern bindings (but unit or tuple structs
// can't).
(None, Pat::Path(name.into()))
}
// shadowing statics is an error as well, so we just ignore that case here

View file

@ -404,3 +404,26 @@ fn foo() {
}"#]]
.assert_eq(&body.pretty_print(&db, def, Edition::CURRENT))
}
#[test]
fn shadowing_record_variant() {
let (_, body, _) = lower(
r#"
enum A {
B { field: i32 },
}
fn f() {
use A::*;
match () {
B => {}
};
}
"#,
);
assert_eq!(body.bindings.len(), 1, "should have a binding for `B`");
assert_eq!(
body.bindings[BindingId::from_raw(RawIdx::from_u32(0))].name.as_str(),
"B",
"should have a binding for `B`",
);
}