Skip to content

Instanceof interface breaks type inference. #810

Description

@tanzanite777

Type: Bug

Code sample to explain the issue:

interface I {
    function i() : void;
}

class B {
    function b() : void {}
}

class C extends B implements I {
    function c() : void {}
    function i() : void {}
}

function bug(B $b) {
    if($b instanceof I) {
        // BUG: $b stopped being an instance of B
        $b->b(); // invalid issue: Call to unknown method: I::b() PHP(PHP0418)
        $b->c(); // correct issue: Call to unknown method: I::c() PHP(PHP0418)
        $b->i();
    }
}

$c = new C();
if($c instanceof I) {
    // BUG: $c stopped being an instance of C (and B)
    $c->b(); // invalid issue: Call to unknown method: I::b() PHP(PHP0418)
    $c->c(); // invalid issue: Call to unknown method: I::c() PHP(PHP0418)
    $c->i();
}

Extension version: 1.56.16884
VS Code version: Code 1.97.2 (e54c774e0add60467559eb0d1e229c6452cf8447, 2025-02-12T23:20:35.343Z)
OS version: Windows_NT x64 10.0.19045
Modes:

System Info
Item Value
CPUs Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz (8 x 3600)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) undefined
Memory (System) 15.88GB (5.53GB free)
Process Argv --crash-reporter-id adf79977-8c83-4303-918e-a7dfc5f31bf1
Screen Reader no
VM 0%
A/B Experiments
vsliv368:30146709
vspor879:30202332
vspor708:30202333
vspor363:30204092
vscod805cf:30301675
binariesv615:30325510
py29gd2263:31024239
c4g48928:30535728
azure-dev_surveyone:30548225
a9j8j154:30646983
962ge761:30959799
h48ei257:31000450
pythontbext0:30879054
cppperfnew:31000557
dwnewjupyter:31046869
nativerepl2:31139839
pythonrstrctxt:31112756
nativeloc2:31192216
iacca1:31171482
5fd0e150:31155592
dwcopilot:31170013
6074i472:31201624
dwoutputs:31242946
customenabled:31248079
hdaa2157:31222309
copilot_t_ci:31222730
jda6j935:31233686
cp15370_c:31242498
copilothoveroff:31248523

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions