1# ArkCompiler子系统Changelog
2
3## cl.arkcompiler.1 ArkTS Sendable&共享模块语法规则编译检查完善
4
5**访问级别**
6
7其他
8
9**变更原因**
10
11Sendable对象需要遵循[使用规则](../../..//application-dev/arkts-utils/arkts-sendable.md#sendable使用规则),共享模块需要遵循[使用规则](../../..//application-dev/arkts-utils/arkts-sendable-module.md#使用规格),在Sendable&共享模块的部分需要约束的export场景中,编译器缺少检查,导致这些场景会发生运行时异常但是没有编译时错误。在本次版本更新中,修复了这些约束的编译时检查,将运行时异常提前到编译时。旨在通过编译时错误或警告,帮助开发者更早发现Sendable&共享模块使用约束,减少运行时定位成本。
12
13**变更影响**
14
15此变更为兼容性变更。
16
17
18变更前:
191. 当在共享模块中使用`export default someVariable`方式导出,并且`someVariable`是Non-sendable类型时,DevEco编辑界面有错误提示,编译没有错误,程序运行时会崩溃。
202. 当在共享模块中使用`export type someType = someNonSendableType`方式导出Non-sendable类型的别名时,DevEco编辑界面没有提示,编译没有异常。
213. 当sendable class内部使用当前模块top level中`export`的sendable class对象时,DevEco编辑界面没有提示,编译没有异常,运行时会触发异常。
22
23变更后:
241. 当在共享模块中使用`export default someVariable`方式导出,并且`someVariable`是Non-sendable类型时,DevEco编辑界面有错误提示,编译有错误。
252. 当在共享模块中使用`export type someType = someNonSendableType`方式导出Non-sendable类型的别名时,DevEco编辑界面有警告提示,编译有警告。
263. 当sendable class内部使用当前模块top level中`export`的sendable class对象时,DevEco编辑界面有警告提示,编译有警告。
27
28
29具体场景示例:
30
31共享模块export约束
32
33场景一:在共享模块中使用`export default someVariable`方式导出,并且`someVariable`是Non-sendable类型时。影响:兼容变更,运行时崩溃变更为编译报错
34
35变更前
36
37```ts
38'use shared';
39class NonSendableClass {};
40export default NonSendableClass; // 引发 GC 时崩溃
41```
42
43变更后
44
45```ts
46'use shared';
47class NonSendableClass {};
48export default NonSendableClass; // 编译错误
49```
50
51场景二:在共享模块中使用`export type someType = someNonSendableType`方式导出Non-sendable的别名时。影响:兼容变更,无提示变更为编辑警告、编译警告
52
53变更前
54
55```ts
56'use shared';
57class NonSendableClass {};
58export type NonSendableAlias = NonSendableClass;
59```
60
61变更后
62
63```ts
64'use shared';
65class NonSendableClass {};
66export type NonSendableAlias = NonSendableClass; // DevEco编辑界面警告提示 & 编译警告
67```
68
69sendable class内部的变量使用约束
70
71场景一:当sendable class内部使用当前模块top level中`export`的sendable class对象时。影响:兼容变更,运行时异常变更为编辑、编译警告
72
73变更前
74
75```ts
76import { taskpool } from '@kit.ArkTS';
77
78@Sendable
79export class SendableData {};
80
81@Sendable
82class SendableClass {
83    handle():void {
84      new SendableData(); // 运行时异常
85    }
86}
87
88@Concurrent
89async function taskHandle(sendable: SendableClass) {
90  sendable.handle();
91}
92
93taskpool.execute(new taskpool.Task(taskHandle, new SendableClass()));
94```
95
96变更后
97
98```ts
99import { taskpool } from '@kit.ArkTS';
100
101@Sendable
102export class SendableData {};
103
104@Sendable
105class SendableClass {
106    handle():void {
107      new SendableData(); // DevEco编辑界面警告提示 & 编译警告
108    }
109}
110
111@Concurrent
112async function taskHandle(sendable: SendableClass) {
113  sendable.handle();
114}
115
116taskpool.execute(new taskpool.Task(taskHandle, new SendableClass()));
117```
118
119**起始 API Level**
120
121ArkTS Sendable语法检查从API 12起启用。
122
123**变更发生版本**
124
125从OpenHarmony SDK 5.0.0.36 开始。
126
127**变更的接口/组件**
128
129不涉及。
130
131**适配指导**
132
133对于开发者,建议按照Sendable&共享模块规格修复新增警告,防止出现运行时异常。
134