Update rustc_pattern_analysis to 0.42.0

This commit is contained in:
Nadrieril 2024-03-07 16:33:31 +01:00
parent e5889c9b43
commit 1b0b4220fd
4 changed files with 86 additions and 61 deletions

37
Cargo.lock generated
View File

@ -588,7 +588,7 @@ dependencies = [
"oorandom", "oorandom",
"project-model", "project-model",
"ra-ap-rustc_abi", "ra-ap-rustc_abi",
"ra-ap-rustc_index", "ra-ap-rustc_index 0.40.0",
"ra-ap-rustc_pattern_analysis", "ra-ap-rustc_pattern_analysis",
"rustc-hash", "rustc-hash",
"scoped-tls", "scoped-tls",
@ -1430,7 +1430,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2548746ce549476a7aa982099dfe5139f939099d83077b3393baf5e920292f64" checksum = "2548746ce549476a7aa982099dfe5139f939099d83077b3393baf5e920292f64"
dependencies = [ dependencies = [
"bitflags 2.4.2", "bitflags 2.4.2",
"ra-ap-rustc_index", "ra-ap-rustc_index 0.40.0",
"tracing", "tracing",
] ]
@ -1441,7 +1441,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "371db64f1be25aae96650b5528c76066ac325f0fe23efec0aab1179d75eea5c3" checksum = "371db64f1be25aae96650b5528c76066ac325f0fe23efec0aab1179d75eea5c3"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"ra-ap-rustc_index_macros", "ra-ap-rustc_index_macros 0.40.0",
"smallvec",
]
[[package]]
name = "ra-ap-rustc_index"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfd7e10c7853fe79443d46e1d2d8ab09fe99926118e59653fb8b480d5045f126"
dependencies = [
"arrayvec",
"ra-ap-rustc_index_macros 0.42.0",
"smallvec", "smallvec",
] ]
@ -1457,6 +1468,18 @@ dependencies = [
"synstructure", "synstructure",
] ]
[[package]]
name = "ra-ap-rustc_index_macros"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47f1d1c589be6c9a9e852fadee0e60329c0f862e87442ac2fe5adae30663cc76"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]] [[package]]
name = "ra-ap-rustc_lexer" name = "ra-ap-rustc_lexer"
version = "0.40.0" version = "0.40.0"
@ -1473,17 +1496,17 @@ version = "0.40.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85f8a94cf47d86b93e7c2440de4308de74e1d5cb71ed3a9df7dcde2550742e6e" checksum = "85f8a94cf47d86b93e7c2440de4308de74e1d5cb71ed3a9df7dcde2550742e6e"
dependencies = [ dependencies = [
"ra-ap-rustc_index", "ra-ap-rustc_index 0.40.0",
"ra-ap-rustc_lexer", "ra-ap-rustc_lexer",
] ]
[[package]] [[package]]
name = "ra-ap-rustc_pattern_analysis" name = "ra-ap-rustc_pattern_analysis"
version = "0.40.0" version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9cb49490fadb58e6b8cc825f73157a33730495bfd5c6e9eef067a4b50ad1836" checksum = "1253da23515d80c377a3998731e0ec3794997b62b989fd47db73efbde6a0bd7c"
dependencies = [ dependencies = [
"ra-ap-rustc_index", "ra-ap-rustc_index 0.42.0",
"rustc-hash", "rustc-hash",
"rustc_apfloat", "rustc_apfloat",
"smallvec", "smallvec",

View File

@ -88,7 +88,7 @@ ra-ap-rustc_lexer = { version = "0.40.0", default-features = false }
ra-ap-rustc_parse_format = { version = "0.40.0", default-features = false } ra-ap-rustc_parse_format = { version = "0.40.0", default-features = false }
ra-ap-rustc_index = { version = "0.40.0", default-features = false } ra-ap-rustc_index = { version = "0.40.0", default-features = false }
ra-ap-rustc_abi = { version = "0.40.0", default-features = false } ra-ap-rustc_abi = { version = "0.40.0", default-features = false }
ra-ap-rustc_pattern_analysis = { version = "0.40.0", default-features = false } ra-ap-rustc_pattern_analysis = { version = "0.42.0", default-features = false }
# local crates that aren't published to crates.io. These should not have versions. # local crates that aren't published to crates.io. These should not have versions.
sourcegen = { path = "./crates/sourcegen" } sourcegen = { path = "./crates/sourcegen" }

View File

@ -239,6 +239,7 @@ impl ExprValidator {
m_arms.as_slice(), m_arms.as_slice(),
scrut_ty.clone(), scrut_ty.clone(),
ValidityConstraint::ValidOnly, ValidityConstraint::ValidOnly,
None,
) { ) {
Ok(report) => report, Ok(report) => report,
Err(()) => return, Err(()) => return,
@ -283,6 +284,7 @@ impl ExprValidator {
&[match_arm], &[match_arm],
ty.clone(), ty.clone(),
ValidityConstraint::ValidOnly, ValidityConstraint::ValidOnly,
None,
) { ) {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {

View File

@ -8,7 +8,7 @@ use rustc_hash::FxHashMap;
use rustc_pattern_analysis::{ use rustc_pattern_analysis::{
constructor::{Constructor, ConstructorSet, VariantVisibility}, constructor::{Constructor, ConstructorSet, VariantVisibility},
index::IdxContainer, index::IdxContainer,
Captures, TypeCx, Captures, PrivateUninhabitedField, TypeCx,
}; };
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use stdx::never; use stdx::never;
@ -88,39 +88,21 @@ impl<'p> MatchCheckCtx<'p> {
} }
} }
// In the cases of either a `#[non_exhaustive]` field list or a non-public field, we hide // This lists the fields of a variant along with their types.
// uninhabited fields in order not to reveal the uninhabitedness of the whole variant. fn list_variant_fields<'a>(
// This lists the fields we keep along with their types.
fn list_variant_nonhidden_fields<'a>(
&'a self, &'a self,
ty: &'a Ty, ty: &'a Ty,
variant: VariantId, variant: VariantId,
) -> impl Iterator<Item = (LocalFieldId, Ty)> + Captures<'a> + Captures<'p> { ) -> impl Iterator<Item = (LocalFieldId, Ty)> + Captures<'a> + Captures<'p> {
let cx = self; let (_, substs) = ty.as_adt().unwrap();
let (adt, substs) = ty.as_adt().unwrap();
let adt_is_local = variant.module(cx.db.upcast()).krate() == cx.module.krate(); let field_tys = self.db.field_types(variant);
let fields_len = variant.variant_data(self.db.upcast()).fields().len() as u32;
// Whether we must not match the fields of this variant exhaustively. (0..fields_len).map(|idx| LocalFieldId::from_raw(idx.into())).map(move |fid| {
let is_non_exhaustive = let ty = field_tys[fid].clone().substitute(Interner, substs);
cx.db.attrs(variant.into()).by_key("non_exhaustive").exists() && !adt_is_local; let ty = normalize(self.db, self.db.trait_environment_for_body(self.body), ty);
(fid, ty)
let visibility = cx.db.field_visibilities(variant);
let field_ty = cx.db.field_types(variant);
let fields_len = variant.variant_data(cx.db.upcast()).fields().len() as u32;
(0..fields_len).map(|idx| LocalFieldId::from_raw(idx.into())).filter_map(move |fid| {
let ty = field_ty[fid].clone().substitute(Interner, substs);
let ty = normalize(cx.db, cx.db.trait_environment_for_body(cx.body), ty);
let is_visible = matches!(adt, hir_def::AdtId::EnumId(..))
|| visibility[fid].is_visible_from(cx.db.upcast(), cx.module);
let is_uninhabited = cx.is_uninhabited(&ty);
if is_uninhabited && (!is_visible || is_non_exhaustive) {
None
} else {
Some((fid, ty))
}
}) })
} }
@ -199,23 +181,16 @@ impl<'p> MatchCheckCtx<'p> {
} }
}; };
let variant = Self::variant_id_for_adt(&ctor, adt.0).unwrap(); let variant = Self::variant_id_for_adt(&ctor, adt.0).unwrap();
let fields_len = variant.variant_data(self.db.upcast()).fields().len(); // Fill a vec with wildcards, then place the fields we have at the right
// For each field in the variant, we store the relevant index into `self.fields` if any. // index.
let mut field_id_to_id: Vec<Option<usize>> = vec![None; fields_len]; let mut wilds: Vec<_> = self
let tys = self .list_variant_fields(&pat.ty, variant)
.list_variant_nonhidden_fields(&pat.ty, variant) .map(|(_, ty)| ty)
.enumerate() .map(DeconstructedPat::wildcard)
.map(|(i, (fid, ty))| { .collect();
let field_idx: u32 = fid.into_raw().into();
field_id_to_id[field_idx as usize] = Some(i);
ty
});
let mut wilds: Vec<_> = tys.map(DeconstructedPat::wildcard).collect();
for pat in subpatterns { for pat in subpatterns {
let field_idx: u32 = pat.field.into_raw().into(); let field_id: u32 = pat.field.into_raw().into();
if let Some(i) = field_id_to_id[field_idx as usize] { wilds[field_id as usize] = self.lower_pat(&pat.pattern);
wilds[i] = self.lower_pat(&pat.pattern);
}
} }
fields = wilds; fields = wilds;
} }
@ -263,7 +238,7 @@ impl<'p> MatchCheckCtx<'p> {
TyKind::Adt(adt, substs) => { TyKind::Adt(adt, substs) => {
let variant = Self::variant_id_for_adt(pat.ctor(), adt.0).unwrap(); let variant = Self::variant_id_for_adt(pat.ctor(), adt.0).unwrap();
let subpatterns = self let subpatterns = self
.list_variant_nonhidden_fields(pat.ty(), variant) .list_variant_fields(pat.ty(), variant)
.zip(subpatterns) .zip(subpatterns)
.map(|((field, _ty), pattern)| FieldPat { field, pattern }) .map(|((field, _ty), pattern)| FieldPat { field, pattern })
.collect(); .collect();
@ -286,7 +261,7 @@ impl<'p> MatchCheckCtx<'p> {
Ref => PatKind::Deref { subpattern: subpatterns.next().unwrap() }, Ref => PatKind::Deref { subpattern: subpatterns.next().unwrap() },
Slice(_) => unimplemented!(), Slice(_) => unimplemented!(),
&Str(void) => match void {}, &Str(void) => match void {},
Wildcard | NonExhaustive | Hidden => PatKind::Wild, Wildcard | NonExhaustive | Hidden | PrivateUninhabited => PatKind::Wild,
Missing | F32Range(..) | F64Range(..) | Opaque(..) | Or => { Missing | F32Range(..) | F64Range(..) | Opaque(..) | Or => {
never!("can't convert to pattern: {:?}", pat.ctor()); never!("can't convert to pattern: {:?}", pat.ctor());
PatKind::Wild PatKind::Wild
@ -326,7 +301,7 @@ impl<'p> TypeCx for MatchCheckCtx<'p> {
1 1
} else { } else {
let variant = Self::variant_id_for_adt(ctor, adt).unwrap(); let variant = Self::variant_id_for_adt(ctor, adt).unwrap();
self.list_variant_nonhidden_fields(ty, variant).count() variant.variant_data(self.db.upcast()).fields().len()
} }
} }
_ => { _ => {
@ -337,7 +312,7 @@ impl<'p> TypeCx for MatchCheckCtx<'p> {
Ref => 1, Ref => 1,
Slice(..) => unimplemented!(), Slice(..) => unimplemented!(),
Bool(..) | IntRange(..) | F32Range(..) | F64Range(..) | Str(..) | Opaque(..) Bool(..) | IntRange(..) | F32Range(..) | F64Range(..) | Str(..) | Opaque(..)
| NonExhaustive | Hidden | Missing | Wildcard => 0, | NonExhaustive | PrivateUninhabited | Hidden | Missing | Wildcard => 0,
Or => { Or => {
never!("The `Or` constructor doesn't have a fixed arity"); never!("The `Or` constructor doesn't have a fixed arity");
0 0
@ -349,13 +324,13 @@ impl<'p> TypeCx for MatchCheckCtx<'p> {
&'a self, &'a self,
ctor: &'a rustc_pattern_analysis::constructor::Constructor<Self>, ctor: &'a rustc_pattern_analysis::constructor::Constructor<Self>,
ty: &'a Self::Ty, ty: &'a Self::Ty,
) -> impl ExactSizeIterator<Item = Self::Ty> + Captures<'a> { ) -> impl ExactSizeIterator<Item = (Self::Ty, PrivateUninhabitedField)> + Captures<'a> {
let single = |ty| smallvec![ty]; let single = |ty| smallvec![(ty, PrivateUninhabitedField(false))];
let tys: SmallVec<[_; 2]> = match ctor { let tys: SmallVec<[_; 2]> = match ctor {
Struct | Variant(_) | UnionField => match ty.kind(Interner) { Struct | Variant(_) | UnionField => match ty.kind(Interner) {
TyKind::Tuple(_, substs) => { TyKind::Tuple(_, substs) => {
let tys = substs.iter(Interner).map(|ty| ty.assert_ty_ref(Interner)); let tys = substs.iter(Interner).map(|ty| ty.assert_ty_ref(Interner));
tys.cloned().collect() tys.cloned().map(|ty| (ty, PrivateUninhabitedField(false))).collect()
} }
TyKind::Ref(.., rty) => single(rty.clone()), TyKind::Ref(.., rty) => single(rty.clone()),
&TyKind::Adt(AdtId(adt), ref substs) => { &TyKind::Adt(AdtId(adt), ref substs) => {
@ -366,7 +341,27 @@ impl<'p> TypeCx for MatchCheckCtx<'p> {
single(subst_ty) single(subst_ty)
} else { } else {
let variant = Self::variant_id_for_adt(ctor, adt).unwrap(); let variant = Self::variant_id_for_adt(ctor, adt).unwrap();
self.list_variant_nonhidden_fields(ty, variant).map(|(_, ty)| ty).collect() let (adt, _) = ty.as_adt().unwrap();
let adt_is_local =
variant.module(self.db.upcast()).krate() == self.module.krate();
// Whether we must not match the fields of this variant exhaustively.
let is_non_exhaustive =
self.db.attrs(variant.into()).by_key("non_exhaustive").exists()
&& !adt_is_local;
let visibilities = self.db.field_visibilities(variant);
self.list_variant_fields(ty, variant)
.map(move |(fid, ty)| {
let is_visible = matches!(adt, hir_def::AdtId::EnumId(..))
|| visibilities[fid]
.is_visible_from(self.db.upcast(), self.module);
let is_uninhabited = self.is_uninhabited(&ty);
let private_uninhabited =
is_uninhabited && (!is_visible || is_non_exhaustive);
(ty, PrivateUninhabitedField(private_uninhabited))
})
.collect()
} }
} }
ty_kind => { ty_kind => {
@ -383,7 +378,7 @@ impl<'p> TypeCx for MatchCheckCtx<'p> {
}, },
Slice(_) => unreachable!("Found a `Slice` constructor in match checking"), Slice(_) => unreachable!("Found a `Slice` constructor in match checking"),
Bool(..) | IntRange(..) | F32Range(..) | F64Range(..) | Str(..) | Opaque(..) Bool(..) | IntRange(..) | F32Range(..) | F64Range(..) | Str(..) | Opaque(..)
| NonExhaustive | Hidden | Missing | Wildcard => smallvec![], | NonExhaustive | PrivateUninhabited | Hidden | Missing | Wildcard => smallvec![],
Or => { Or => {
never!("called `Fields::wildcards` on an `Or` ctor"); never!("called `Fields::wildcards` on an `Or` ctor");
smallvec![] smallvec![]
@ -478,6 +473,11 @@ impl<'p> TypeCx for MatchCheckCtx<'p> {
fn bug(&self, fmt: fmt::Arguments<'_>) { fn bug(&self, fmt: fmt::Arguments<'_>) {
debug!("{}", fmt) debug!("{}", fmt)
} }
fn complexity_exceeded(&self) -> Result<(), Self::Error> {
// FIXME(Nadrieril): make use of the complexity counter.
Err(())
}
} }
impl<'p> fmt::Debug for MatchCheckCtx<'p> { impl<'p> fmt::Debug for MatchCheckCtx<'p> {