Skip to content

Commit 5f58e94

Browse files
committed
fix(analyzer): don't flag concrete-to-template type assertions as redundant
Signed-off-by: azjezz <[email protected]>
1 parent 6ec581b commit 5f58e94

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

crates/analyzer/src/invocation/post_process.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ fn resolve_invocation_assertion<'ctx, 'ast, 'arena>(
412412
if always_redundant
413413
&& let Some(asserted_type) = &asserted_type
414414
&& !asserted_type.is_mixed()
415+
&& !resolved_assertion_type.has_template()
415416
{
416417
let mut comparison_result = ComparisonResult::default();
417418
let is_subtype = is_contained_by(
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
* @template T of array-key
5+
*/
6+
interface TypeInterface
7+
{
8+
/**
9+
* @return T
10+
*/
11+
public function assert(mixed $value): mixed;
12+
}
13+
14+
/**
15+
* @template T of array-key
16+
*
17+
* @param TypeInterface<T> $type
18+
* @param array-key $value
19+
*
20+
* @return T
21+
*/
22+
function narrow_to_t(TypeInterface $type, mixed $value): mixed
23+
{
24+
return $type->assert($value);
25+
}

crates/analyzer/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ test_case!(accessing_undefined_class_constant);
3535
test_case!(argument_count);
3636
test_case!(array_list_reconciliation);
3737
test_case!(array_shape_fields);
38+
test_case!(assert_concrete_to_template_type);
3839
test_case!(assert_generic_array_key_is_array_key);
3940
test_case!(bare_identifier_in_array_access);
4041
test_case!(by_reference_invalidation);

0 commit comments

Comments
 (0)