Skip to content

Commit 2a69df2

Browse files
committed
Be more conservative concerning structural_match
1 parent f37d606 commit 2a69df2

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/librustc_mir_build/hair/pattern/const_to_pat.rs

+9
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,18 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
123123
traits::NonStructuralMatchTy::Dynamic => {
124124
"trait objects cannot be used in patterns".to_string()
125125
}
126+
traits::NonStructuralMatchTy::Opaque => {
127+
"opaque types cannot be used in patterns".to_string()
128+
}
129+
traits::NonStructuralMatchTy::Generator => {
130+
"generators cannot be used in patterns".to_string()
131+
}
126132
traits::NonStructuralMatchTy::Param => {
127133
bug!("use of a constant whose type is a parameter inside a pattern")
128134
}
135+
traits::NonStructuralMatchTy::Projection => {
136+
bug!("use of a constant whose type is a projection inside a pattern")
137+
}
129138
traits::NonStructuralMatchTy::Foreign => {
130139
bug!("use of a value of a foreign type inside a pattern")
131140
}

src/librustc_trait_selection/traits/structural_match.rs

+19-17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub enum NonStructuralMatchTy<'tcx> {
1313
Param,
1414
Dynamic,
1515
Foreign,
16+
Opaque,
17+
Generator,
18+
Projection,
1619
}
1720

1821
/// This method traverses the structure of `ty`, trying to find an
@@ -147,6 +150,18 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
147150
self.found = Some(NonStructuralMatchTy::Foreign);
148151
return true; // Stop visiting
149152
}
153+
ty::Opaque(..) => {
154+
self.found = Some(NonStructuralMatchTy::Opaque);
155+
return true;
156+
}
157+
ty::Projection(..) => {
158+
self.found = Some(NonStructuralMatchTy::Projection);
159+
return true;
160+
}
161+
ty::Generator(..) | ty::GeneratorWitness(..) => {
162+
self.found = Some(NonStructuralMatchTy::Generator);
163+
return true;
164+
}
150165
ty::RawPtr(..) => {
151166
// structural-match ignores substructure of
152167
// `*const _`/`*mut _`, so skip `super_visit_with`.
@@ -180,39 +195,26 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
180195
// for empty array.
181196
return false;
182197
}
183-
ty::Bool
184-
| ty::Char
185-
| ty::Int(_)
186-
| ty::Uint(_)
187-
| ty::Float(_)
188-
| ty::Str
189-
| ty::Never => {
198+
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str | ty::Never => {
190199
// These primitive types are always structural match.
191200
//
192201
// `Never` is kind of special here, but as it is not inhabitable, this should be fine.
193202
return false;
194203
}
195204

196-
ty::Array(..)
197-
| ty::Slice(_)
198-
| ty::Ref(..)
199-
| ty::Closure(..)
200-
| ty::Generator(..)
201-
| ty::Tuple(..)
202-
| ty::Projection(..)
203-
| ty::Opaque(..)
204-
| ty::GeneratorWitness(..) => {
205+
ty::Array(..) | ty::Slice(_) | ty::Ref(..) | ty::Tuple(..) => {
205206
ty.super_visit_with(self);
206207
return false;
207208
}
209+
ty::Closure(..)
208210
| ty::Infer(_)
209211
| ty::Placeholder(_)
210212
| ty::UnnormalizedProjection(..)
211213
| ty::Bound(..) => {
212214
bug!("unexpected type during structural-match checking: {:?}", ty);
213215
}
214216
ty::Error => {
215-
self.tcx().delay_span_bug(self.span, "ty::Error in structural-match check");
217+
self.tcx().sess.delay_span_bug(self.span, "ty::Error in structural-match check");
216218
// We still want to check other types after encountering an error,
217219
// as this may still emit relevant errors.
218220
return false;

0 commit comments

Comments
 (0)