已合并
instanceof+interface unexpected narrows to never #11175
instanceof+interface unexpected narrows to never #11175
已合并
ajq257257创建于 14 天前
ajq257257
14 天前

关联的Issue

https://gitcode.com/openharmony/arkcompiler_ets_frontend/issues/11941

提交类型

需求背景/Description

问题现象&&分析/Reason

interface Conv02006i {
  i1: int;
}
class Conv02006a {
  a1: int = 1;
}
class Conv02006 extends Conv02006a implements Conv02006i {
  i1: int = 1;
}
function run(): int {
  let c0: Conv02006 = new Conv02006();
  let c1: Conv02006i = c0 as Conv02006i;
  let c2: Conv02006a;
  try {
    if(c1 instanceof Conv02006a){
        c2 = c1 as Conv02006a;
    }
  } catch (e) {
    return 2
  }
  return 0;
}
arktest.assertEQ(run(), 0);     // error: 2 === 0

In ArkTS-Sta, the checker performs smart-cast (type narrowing) inside the true branch of if (x instanceof T). When the static type of x is an interface and T is an unrelated class (or vice versa), the checker incorrectly narrowed x to never. The codegen then emitted failedTypeCastInclUndefinedStub + throw inside the if-body, so when instanceof correctly returned true at runtime, the unconditional throw was triggered and caught by a surrounding try/catch, making the program take the wrong branch.

reason

Two bugs in the ETS checker, both from the same misconception: when two types have no direct supertype relationship, the checker assumed instanceof can never succeed — ignoring that an interface-typed variable can hold an object whose concrete class extends the tested type.

  1. CheckTestObjectCondition (helpers.cpp) — In the fallthrough case (neither type is a supertype of the other), it unconditionally returned {never, actualType} for the true/false branches. This is only valid for non-interface classes (single inheritance); for interfaces, instanceof can still succeed. The never type then caused EmitFailedTypeCastException → failedTypeCastInclUndefinedStub + throw.
  2. ReportIfInstanceofTrivialKnown (arithmetic.cpp) — The "always false" warning (W1001506) used || instead of &&, emitting a spurious warning whenever either side (instead of neither side) was not an interface.

修改方案/Scheme

  1. helpers.cpp: Before the {never, actualType} fallthrough, added a guard — if at least one type is an interface, return {testedType, actualType}, narrowing to the tested type instead of never.
  2. arithmetic.cpp: Changed || to && so the warning fires only when neither side has an interface constituent.
    After the fix, codegen produces a normal checkcast.nonnull instead of failedTypeCastInclUndefinedStub + throw, and the runtime check succeeds as expected.

测试结果(测试截图直接贴在对应测试项,主干已知问题需明确引入pr/责任人)

功能测试(除仅涉及文本外必测项)wiki

  1. es2abc测试用例(Debug模式)
  1. Verifier测试
  1. 64位RK编译
  1. 编译mac平台sdk

混淆测试(涉及arkguard改动时必测项)wiki

  1. 单元测试

  2. Compiler测试套

  3. TSC extra测试套

  4. Test262测试套

  5. Benchmark测试

  6. 应用自动化测试套

  7. 是否创建全局变量,若创建全局变量,是否有清空操作

兼容性测试(指令生成、文件格式修改时)

  1. 小版本兼容性测试

  2. 大版本兼容性测试

  3. es2abc版本兼容性测试
    说明:如PR涉及版本控制用例的变更,需同步检查修改对其他分支的影响,并确认是否要同步受影响的分支

  4. 是否涉及词法环境修改
    说明:如PR涉及词法环境修改,需验证对应热重载场景是否兼容

性能测试 (新增语法检查等场景)

指令/abc格式修改自检,需联系下方邮箱,同步至相关领域

重要:涉及runtime_core仓abc2program、libpandafile、isa目录下的修改,必须提供一个编译helloworld项目的hap包给对应领域,并联系下方邮箱

Email: wutao185@huawei.com

是否已执行L0用例

是否已执行L0用例

Change-Id: Ib53fd18cbf4dd1efa983818d9810e59fd6a3f01e

likedislike
Pull Request已成功合入, 合并人@openharmony_ci
(感谢 ajq257257 的贡献)
Aajq257257
14 天前 关联了issue:[Bug]: instanceof+interface unexpected narrows to never
ajq257257
14 天前 评论:

start build

likedislike
openharmony_ci
openharmony_ci成员
14 天前 评论:

该仓未配置门禁,无法触发构建。如需要检视代码请 pr作者 评论 code review

likedislike
ajq257257
14 天前 评论:

start build

likedislike
openharmony_ci
openharmony_ci成员
14 天前 评论:

首次触发
门禁构建开始,包含静态检查、代码编译和测试【dayu200_xts_acts编译, ohos-host_mini_tdd编译, dayu600_7885编译, dayu200测试, dayu600_7885测试, hap_build编译, dayu200编译, mac-sdk编译, dayu200_tdd编译, master_inner_build编译, x86_64_virt编译, ohos-sdk编译, dayu200_xts编译, dayu200_xts_static编译】,预计在60分钟内完成,门禁结果会同步发送到注册邮箱。您可以通过如下链接跟踪门禁进展:http://dcp.openharmony.cn/workbench/cicd/detail/6a7ec0f364650f998b7f5b98/runlist

likedislike
此处折叠了54条消息 查看更多
ajq257257
11 天前 评论:

submit

likedislike
openharmony_ciopenharmony_ci成员
11 天前 关闭了关联的issue
openharmony_ciopenharmony_ci成员
11 天前 合入了pull request,合并节点 SHA:4acda50900c0a0b47b167a13b675476e3f2ac194
openharmony_ciopenharmony_ci成员
11 天前 删除了label:waiting_for_review
openharmony_ciopenharmony_ci成员
11 天前 添加了label:merged