Skip to content

Commit 5a5017e

Browse files
committed
Be more conservative concerning structural_match
1 parent ecab35b commit 5a5017e

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

src/librustc_mir_build/hair/pattern/const_to_pat.rs

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

src/librustc_trait_selection/traits/structural_match.rs

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

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

197-
ty::Array(..)
198-
| ty::Slice(_)
199-
| ty::Ref(..)
200-
| ty::Closure(..)
201-
| ty::Generator(..)
202-
| ty::Tuple(..)
203-
| ty::Projection(..)
204-
| ty::Opaque(..)
205-
| ty::GeneratorWitness(..) => {
206+
ty::Array(..) | ty::Slice(_) | ty::Ref(..) | ty::Tuple(..) => {
206207
ty.super_visit_with(self);
207208
return false;
208209
}
209-
| ty::Infer(_)
210-
| ty::Placeholder(_)
211-
| ty::UnnormalizedProjection(..)
212-
| ty::Bound(..) => {
210+
ty::Closure(..) | ty::Infer(_) | ty::Placeholder(_) | ty::Bound(..) => {
213211
bug!("unexpected type during structural-match checking: {:?}", ty);
214212
}
215213
ty::Error => {
216-
self.tcx().delay_span_bug(self.span, "ty::Error in structural-match check");
214+
self.tcx().sess.delay_span_bug(self.span, "ty::Error in structural-match check");
217215
// We still want to check other types after encountering an error,
218216
// as this may still emit relevant errors.
219217
return false;

0 commit comments

Comments
 (0)