]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir_build/hair/pattern/const_to_pat.rs
Rollup merge of #72401 - ecstatic-morse:issue-72394, r=eddyb
[rust.git] / src / librustc_mir_build / hair / pattern / const_to_pat.rs
1 use rustc_hir as hir;
2 use rustc_hir::lang_items::EqTraitLangItem;
3 use rustc_index::vec::Idx;
4 use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
5 use rustc_middle::mir::Field;
6 use rustc_middle::ty::{self, Ty, TyCtxt};
7 use rustc_session::lint;
8 use rustc_span::Span;
9 use rustc_trait_selection::traits::predicate_for_trait_def;
10 use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
11 use rustc_trait_selection::traits::{self, ObligationCause, PredicateObligation};
12
13 use std::cell::Cell;
14
15 use super::{FieldPat, Pat, PatCtxt, PatKind};
16
17 impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
18     /// Converts an evaluated constant to a pattern (if possible).
19     /// This means aggregate values (like structs and enums) are converted
20     /// to a pattern that matches the value (as if you'd compared via structural equality).
21     pub(super) fn const_to_pat(
22         &self,
23         cv: &'tcx ty::Const<'tcx>,
24         id: hir::HirId,
25         span: Span,
26         mir_structural_match_violation: bool,
27     ) -> Pat<'tcx> {
28         debug!("const_to_pat: cv={:#?} id={:?}", cv, id);
29         debug!("const_to_pat: cv.ty={:?} span={:?}", cv.ty, span);
30
31         self.tcx.infer_ctxt().enter(|infcx| {
32             let mut convert = ConstToPat::new(self, id, span, infcx);
33             convert.to_pat(cv, mir_structural_match_violation)
34         })
35     }
36 }
37
38 struct ConstToPat<'a, 'tcx> {
39     id: hir::HirId,
40     span: Span,
41     param_env: ty::ParamEnv<'tcx>,
42
43     // This tracks if we signal some hard error for a given const value, so that
44     // we will not subsequently issue an irrelevant lint for the same const
45     // value.
46     saw_const_match_error: Cell<bool>,
47
48     // inference context used for checking `T: Structural` bounds.
49     infcx: InferCtxt<'a, 'tcx>,
50
51     include_lint_checks: bool,
52 }
53
54 impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
55     fn new(
56         pat_ctxt: &PatCtxt<'_, 'tcx>,
57         id: hir::HirId,
58         span: Span,
59         infcx: InferCtxt<'a, 'tcx>,
60     ) -> Self {
61         ConstToPat {
62             id,
63             span,
64             infcx,
65             param_env: pat_ctxt.param_env,
66             include_lint_checks: pat_ctxt.include_lint_checks,
67             saw_const_match_error: Cell::new(false),
68         }
69     }
70
71     fn tcx(&self) -> TyCtxt<'tcx> {
72         self.infcx.tcx
73     }
74
75     fn search_for_structural_match_violation(
76         &self,
77         ty: Ty<'tcx>,
78     ) -> Option<traits::NonStructuralMatchTy<'tcx>> {
79         traits::search_for_structural_match_violation(self.id, self.span, self.tcx(), ty)
80     }
81
82     fn type_marked_structural(&self, ty: Ty<'tcx>) -> bool {
83         traits::type_marked_structural(self.id, self.span, &self.infcx, ty)
84     }
85
86     fn to_pat(
87         &mut self,
88         cv: &'tcx ty::Const<'tcx>,
89         mir_structural_match_violation: bool,
90     ) -> Pat<'tcx> {
91         // This method is just a wrapper handling a validity check; the heavy lifting is
92         // performed by the recursive `recur` method, which is not meant to be
93         // invoked except by this method.
94         //
95         // once indirect_structural_match is a full fledged error, this
96         // level of indirection can be eliminated
97
98         let inlined_const_as_pat = self.recur(cv);
99
100         if self.include_lint_checks && !self.saw_const_match_error.get() {
101             // If we were able to successfully convert the const to some pat,
102             // double-check that all types in the const implement `Structural`.
103
104             let structural = self.search_for_structural_match_violation(cv.ty);
105             debug!(
106                 "search_for_structural_match_violation cv.ty: {:?} returned: {:?}",
107                 cv.ty, structural
108             );
109
110             if structural.is_none() && mir_structural_match_violation {
111                 bug!("MIR const-checker found novel structural match violation");
112             }
113
114             if let Some(non_sm_ty) = structural {
115                 let msg = match non_sm_ty {
116                     traits::NonStructuralMatchTy::Adt(adt_def) => {
117                         let path = self.tcx().def_path_str(adt_def.did);
118                         format!(
119                             "to use a constant of type `{}` in a pattern, \
120                              `{}` must be annotated with `#[derive(PartialEq, Eq)]`",
121                             path, path,
122                         )
123                     }
124                     traits::NonStructuralMatchTy::Dynamic => {
125                         "trait objects cannot be used in patterns".to_string()
126                     }
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                     }
133                     traits::NonStructuralMatchTy::Param => {
134                         bug!("use of a constant whose type is a parameter inside a pattern")
135                     }
136                     traits::NonStructuralMatchTy::Projection => {
137                         bug!("use of a constant whose type is a projection inside a pattern")
138                     }
139                     traits::NonStructuralMatchTy::Foreign => {
140                         bug!("use of a value of a foreign type inside a pattern")
141                     }
142                 };
143
144                 // double-check there even *is* a semantic `PartialEq` to dispatch to.
145                 //
146                 // (If there isn't, then we can safely issue a hard
147                 // error, because that's never worked, due to compiler
148                 // using `PartialEq::eq` in this scenario in the past.)
149                 //
150                 // Note: To fix rust-lang/rust#65466, one could lift this check
151                 // *before* any structural-match checking, and unconditionally error
152                 // if `PartialEq` is not implemented. However, that breaks stable
153                 // code at the moment, because types like `for <'a> fn(&'a ())` do
154                 // not *yet* implement `PartialEq`. So for now we leave this here.
155                 let ty_is_partial_eq: bool = {
156                     let partial_eq_trait_id =
157                         self.tcx().require_lang_item(EqTraitLangItem, Some(self.span));
158                     let obligation: PredicateObligation<'_> = predicate_for_trait_def(
159                         self.tcx(),
160                         self.param_env,
161                         ObligationCause::misc(self.span, self.id),
162                         partial_eq_trait_id,
163                         0,
164                         cv.ty,
165                         &[],
166                     );
167                     // FIXME: should this call a `predicate_must_hold` variant instead?
168                     self.infcx.predicate_may_hold(&obligation)
169                 };
170
171                 if !ty_is_partial_eq {
172                     // span_fatal avoids ICE from resolution of non-existent method (rare case).
173                     self.tcx().sess.span_fatal(self.span, &msg);
174                 } else if mir_structural_match_violation {
175                     self.tcx().struct_span_lint_hir(
176                         lint::builtin::INDIRECT_STRUCTURAL_MATCH,
177                         self.id,
178                         self.span,
179                         |lint| lint.build(&msg).emit(),
180                     );
181                 } else {
182                     debug!(
183                         "`search_for_structural_match_violation` found one, but `CustomEq` was \
184                           not in the qualifs for that `const`"
185                     );
186                 }
187             }
188         }
189
190         inlined_const_as_pat
191     }
192
193     // Recursive helper for `to_pat`; invoke that (instead of calling this directly).
194     fn recur(&self, cv: &'tcx ty::Const<'tcx>) -> Pat<'tcx> {
195         let id = self.id;
196         let span = self.span;
197         let tcx = self.tcx();
198         let param_env = self.param_env;
199
200         let field_pats = |vals: &[&'tcx ty::Const<'tcx>]| {
201             vals.iter()
202                 .enumerate()
203                 .map(|(idx, val)| {
204                     let field = Field::new(idx);
205                     FieldPat { field, pattern: self.recur(val) }
206                 })
207                 .collect()
208         };
209
210         let kind = match cv.ty.kind {
211             ty::Float(_) => {
212                 tcx.struct_span_lint_hir(
213                     lint::builtin::ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
214                     id,
215                     span,
216                     |lint| lint.build("floating-point types cannot be used in patterns").emit(),
217                 );
218                 PatKind::Constant { value: cv }
219             }
220             ty::Adt(adt_def, _) if adt_def.is_union() => {
221                 // Matching on union fields is unsafe, we can't hide it in constants
222                 self.saw_const_match_error.set(true);
223                 tcx.sess.span_err(span, "cannot use unions in constant patterns");
224                 PatKind::Wild
225             }
226             // keep old code until future-compat upgraded to errors.
227             ty::Adt(adt_def, _) if !self.type_marked_structural(cv.ty) => {
228                 debug!("adt_def {:?} has !type_marked_structural for cv.ty: {:?}", adt_def, cv.ty);
229                 let path = tcx.def_path_str(adt_def.did);
230                 let msg = format!(
231                     "to use a constant of type `{}` in a pattern, \
232                      `{}` must be annotated with `#[derive(PartialEq, Eq)]`",
233                     path, path,
234                 );
235                 self.saw_const_match_error.set(true);
236                 tcx.sess.span_err(span, &msg);
237                 PatKind::Wild
238             }
239             // keep old code until future-compat upgraded to errors.
240             ty::Ref(_, adt_ty @ ty::TyS { kind: ty::Adt(_, _), .. }, _)
241                 if !self.type_marked_structural(adt_ty) =>
242             {
243                 let adt_def =
244                     if let ty::Adt(adt_def, _) = adt_ty.kind { adt_def } else { unreachable!() };
245
246                 debug!(
247                     "adt_def {:?} has !type_marked_structural for adt_ty: {:?}",
248                     adt_def, adt_ty
249                 );
250
251                 // HACK(estebank): Side-step ICE #53708, but anything other than erroring here
252                 // would be wrong. Returnging `PatKind::Wild` is not technically correct.
253                 let path = tcx.def_path_str(adt_def.did);
254                 let msg = format!(
255                     "to use a constant of type `{}` in a pattern, \
256                      `{}` must be annotated with `#[derive(PartialEq, Eq)]`",
257                     path, path,
258                 );
259                 self.saw_const_match_error.set(true);
260                 tcx.sess.span_err(span, &msg);
261                 PatKind::Wild
262             }
263             ty::Adt(adt_def, substs) if adt_def.is_enum() => {
264                 let destructured = tcx.destructure_const(param_env.and(cv));
265                 PatKind::Variant {
266                     adt_def,
267                     substs,
268                     variant_index: destructured.variant,
269                     subpatterns: field_pats(destructured.fields),
270                 }
271             }
272             ty::Adt(_, _) => {
273                 let destructured = tcx.destructure_const(param_env.and(cv));
274                 PatKind::Leaf { subpatterns: field_pats(destructured.fields) }
275             }
276             ty::Tuple(_) => {
277                 let destructured = tcx.destructure_const(param_env.and(cv));
278                 PatKind::Leaf { subpatterns: field_pats(destructured.fields) }
279             }
280             ty::Array(..) => PatKind::Array {
281                 prefix: tcx
282                     .destructure_const(param_env.and(cv))
283                     .fields
284                     .iter()
285                     .map(|val| self.recur(val))
286                     .collect(),
287                 slice: None,
288                 suffix: Vec::new(),
289             },
290             _ => PatKind::Constant { value: cv },
291         };
292
293         Pat { span, ty: cv.ty, kind: Box::new(kind) }
294     }
295 }