@@ -107,11 +107,37 @@ impl<'tcx> ConstToPat<'tcx> {
107107 self . tcx . erase_and_anonymize_regions ( self . typing_env ) . with_codegen_normalized ( self . tcx ) ;
108108 let alias_const = self . tcx . erase_and_anonymize_regions ( alias_const) ;
109109
110+ let mk_too_generic_err = || {
111+ let mut err = self
112+ . tcx
113+ . dcx ( )
114+ . create_err ( ConstPatternDependsOnGenericParameter { span : self . span } ) ;
115+ for arg in alias_const. args {
116+ if let ty:: GenericArgKind :: Type ( ty) = arg. kind ( )
117+ && let ty:: Param ( param_ty) = ty. kind ( )
118+ {
119+ let def_id = self . tcx . hir_enclosing_body_owner ( self . id ) ;
120+ let generics = self . tcx . generics_of ( def_id) ;
121+ let param = generics. type_param ( * param_ty, self . tcx ) ;
122+ let span = self . tcx . def_span ( param. def_id ) ;
123+ err. span_label ( span, "constant depends on this generic parameter" ) ;
124+ if let Some ( ident) = self . tcx . def_ident_span ( def_id)
125+ && self . tcx . sess . source_map ( ) . is_multiline ( ident. between ( span) )
126+ {
127+ // Display the `fn` name as well in the diagnostic, as the generic isn't
128+ // in the same line and it could be confusing otherwise.
129+ err. span_label ( ident, "" ) ;
130+ }
131+ }
132+ }
133+ return self . mk_err ( err, ty) ;
134+ } ;
135+
110136 // FIXME(gca): This will become insufficient once associated constants can be
111137 // implemented as `type` consts (project-const-generics#76). At that point it'll
112138 // become necessary to just use type system normalization for all const patterns
113139 // but that's not yet possible.
114- let mut thir_pat = if alias_const. kind . is_type_const ( self . tcx ) {
140+ let const_value = if alias_const. kind . is_type_const ( self . tcx ) {
115141 let Ok ( normalize) = self
116142 . tcx
117143 . try_normalize_erasing_regions ( self . typing_env , Unnormalized :: new_wip ( self . c ) )
@@ -124,7 +150,7 @@ impl<'tcx> ConstToPat<'tcx> {
124150 let err = self . tcx . dcx ( ) . create_err ( CouldNotEvalConstPattern { span : self . span } ) ;
125151 return self . mk_err ( err, ty) ;
126152 } ;
127- self . valtree_to_pat ( value)
153+ value
128154 } else {
129155 // try to resolve e.g. associated constants to their definition on an impl, and then
130156 // evaluate the const.
@@ -147,29 +173,7 @@ impl<'tcx> ConstToPat<'tcx> {
147173 return self . mk_err ( err, ty) ;
148174 }
149175 Err ( ErrorHandled :: TooGeneric ( _) ) => {
150- let mut err = self
151- . tcx
152- . dcx ( )
153- . create_err ( ConstPatternDependsOnGenericParameter { span : self . span } ) ;
154- for arg in alias_const. args {
155- if let ty:: GenericArgKind :: Type ( ty) = arg. kind ( )
156- && let ty:: Param ( param_ty) = ty. kind ( )
157- {
158- let def_id = self . tcx . hir_enclosing_body_owner ( self . id ) ;
159- let generics = self . tcx . generics_of ( def_id) ;
160- let param = generics. type_param ( * param_ty, self . tcx ) ;
161- let span = self . tcx . def_span ( param. def_id ) ;
162- err. span_label ( span, "constant depends on this generic parameter" ) ;
163- if let Some ( ident) = self . tcx . def_ident_span ( def_id)
164- && self . tcx . sess . source_map ( ) . is_multiline ( ident. between ( span) )
165- {
166- // Display the `fn` name as well in the diagnostic, as the generic isn't
167- // in the same line and it could be confusing otherwise.
168- err. span_label ( ident, "" ) ;
169- }
170- }
171- }
172- return self . mk_err ( err, ty) ;
176+ return mk_too_generic_err ( ) ;
173177 }
174178 Ok ( Err ( bad_ty) ) => {
175179 // The pattern cannot be turned into a valtree.
@@ -192,8 +196,12 @@ impl<'tcx> ConstToPat<'tcx> {
192196 } ;
193197
194198 // Lower the valtree to a THIR pattern.
195- self . valtree_to_pat ( ty:: Value { ty, valtree } )
199+ ty:: Value { ty, valtree }
196200 } ;
201+ if const_value. ty . has_param ( ) {
202+ return mk_too_generic_err ( ) ;
203+ }
204+ let mut thir_pat = self . valtree_to_pat ( const_value) ;
197205
198206 if !thir_pat. references_error ( ) {
199207 // Always check for `PartialEq` if we had no other errors yet.
0 commit comments