@@ -340,7 +340,10 @@ public static function checkParams(
340340 $ selfVerifiedParams = [];
341341
342342 if ($ allTemplates !== [] && $ paramsUseGenerics && ($ contract ['paramsNeedPreInference ' ] ?? true )) {
343- self ::preInferGenericTemplates ($ contract ['types ' ], $ vars , $ effectiveFunction , $ thisObj , $ allTemplates , $ classTemplates , $ selfVerifiedParams );
343+ $ preInferErr = self ::preInferGenericTemplates ($ contract ['types ' ], $ vars , $ effectiveFunction , $ thisObj , $ allTemplates , $ classTemplates , $ selfVerifiedParams );
344+ if ($ preInferErr !== null ) {
345+ return $ preInferErr ;
346+ }
344347 }
345348
346349 $ boundTemplates = [];
@@ -815,17 +818,20 @@ private static function preInferGenericTemplates(
815818 array $ templates ,
816819 array $ classTemplates = [],
817820 array &$ selfVerifiedParams = []
818- ): void {
821+ ): ? ErrorMessage {
819822 if ($ types === []) {
820- return ;
823+ return null ;
821824 }
822825
823826 $ mask = self ::$ preInferMaskCache [$ effectiveFunction ] ??= self ::computePreInferMask ($ types );
824827
825828 if (($ mask & 1 ) !== 0 ) {
826829 foreach ($ vars as $ v ) {
827830 if ($ v instanceof \Closure) {
828- self ::inferTemplatesFromClosures ($ types , $ vars , $ effectiveFunction , $ thisObj , $ templates , $ classTemplates );
831+ $ err = self ::inferTemplatesFromClosures ($ types , $ vars , $ effectiveFunction , $ thisObj , $ templates , $ classTemplates );
832+ if ($ err !== null ) {
833+ return $ err ;
834+ }
829835
830836 break ;
831837 }
@@ -843,6 +849,8 @@ private static function preInferGenericTemplates(
843849 if (($ mask & 8 ) !== 0 ) {
844850 self ::inferTemplatesFromGenericObjects ($ types , $ vars , $ effectiveFunction , $ thisObj , $ templates , $ classTemplates );
845851 }
852+
853+ return null ;
846854 }
847855
848856 /**
@@ -930,10 +938,10 @@ private static function inferTemplatesFromClosures(
930938 ?object $ thisObj ,
931939 array $ templates ,
932940 array $ classTemplates = []
933- ): void {
941+ ): ? ErrorMessage {
934942 $ callableNodes = self ::callableNodesFor ($ effectiveFunction , $ types );
935943 if (\count ($ callableNodes ) === 0 ) {
936- return ;
944+ return null ;
937945 }
938946
939947 foreach ($ callableNodes as $ cParamName => $ cTypeNode ) {
@@ -956,9 +964,31 @@ private static function inferTemplatesFromClosures(
956964 if ($ inferredCandidate !== null ) {
957965 if ($ targetObj !== null ) {
958966 $ existingType = TemplateManager ::getInstanceBoundTypeFast ($ targetObj , $ tName );
959- if ($ existingType !== null && TemplateManager ::checkVariance ($ existingType , $ inferredCandidate , GenericTypeNode::VARIANCE_COVARIANT )) {
960- continue ;
967+ if ($ existingType !== null ) {
968+ $ canRefine = TemplateManager ::isFallbackBound ($ targetObj , $ tName )
969+ || TemplateManager ::isUnspecializedType ($ existingType );
970+
971+ if ($ canRefine ) {
972+ if (
973+ TemplateManager ::checkVariance ($ existingType , $ inferredCandidate , GenericTypeNode::VARIANCE_COVARIANT )
974+ || ! TemplateManager ::checkVariance ($ inferredCandidate , $ existingType , GenericTypeNode::VARIANCE_COVARIANT )
975+ ) {
976+ continue ;
977+ }
978+ } else {
979+ if (! TemplateManager ::checkVariance ($ existingType , $ inferredCandidate , GenericTypeNode::VARIANCE_COVARIANT )) {
980+ $ cParamLabel = $ closureParams [$ idx ]->getName ();
981+
982+ return ErrorFactory ::createError (
983+ $ effectiveFunction . '(): Argument $ ' . $ cParamName . ' callback parameter $ ' . $ cParamLabel . ' must be ' . $ existingType . ' (or supertype), ' . $ inferredCandidate . ' given '
984+ );
985+ }
986+
987+ continue ;
988+ }
961989 }
990+ } elseif (TemplateManager ::isBound ($ effectiveFunction , null , $ tName )) {
991+ continue ;
962992 }
963993
964994 $ templateTag = $ templates [$ tName ];
@@ -988,6 +1018,8 @@ private static function inferTemplatesFromClosures(
9881018 // Silently ignore reflection errors
9891019 }
9901020 }
1021+
1022+ return null ;
9911023 }
9921024
9931025 /**
0 commit comments