当前位置: 首页 > news >正文

从 Kotlin 编译器 API 的变化开始: 2.2.2X -> 2.3.0-Beta1

大家好!众所周知,我有在平时维护一个简单的Kotlin编译器插件项目:
Kotlin Suspend Transform Compiler Plugin。
想必经常维护编译器插件的小伙伴们也清楚,每次 Kotlin 的主要版本递进,编译器的API都会或多或少的发生变化,
也给编译器插件的更新维护带来不小的挑战。那么借此机会,我会在每次发生API变化的更新出现后,
籍由此系列记录一下能有哪些编译器API的变化可以被我发现。

不算是一种技术分享文,所以不保证有什么技术含金量喔~

今天要记录的版本变化是:v2.2.2X -> 2.3.0-Beta1


首先先直接列举一下这次更新出现的所有错误和警告:

[KOTLIN] e: file:///Users/forte/IdeaProjects/suspend-transform-kotlin-compile-plugin/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/SuspendTransformComponentRegistrar.kt:38:1 Class 'SuspendTransformComponentRegistrar' is not abstract and does not implement abstract base class member:
val pluginId: String
[KOTLIN] e: file:///suspend-transform-kotlin-compile-plugin/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/fir/SuspendTransformFirTransformer.kt:40:51 Unresolved reference 'getContainingClassSymbol'.
[KOTLIN] e: file:///suspend-transform-kotlin-compile-plugin/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/fir/SuspendTransformFirTransformer.kt:891:84 Unresolved reference 'getContainingClassSymbol'.
[KOTLIN] e: file:///suspend-transform-kotlin-compile-plugin/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/ir/SuspendTransformTransformer.kt:406:34 'val symbols: Symbols' is deprecated. This API is deprecated. Use `irBuiltIns` instead.
[KOTLIN] e: file:///suspend-transform-kotlin-compile-plugin/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/ir/SuspendTransformTransformer.kt:462:38 'val symbols: Symbols' is deprecated. This API is deprecated. Use `irBuiltIns` instead.

看来这次变化的东西不是非常多,也是很仁慈了。接下来一个个来看看吧~

SuspendTransformComponentRegistrar.pluginId

更新到 2.3.0 之后,SuspendTransformComponentRegistrar 新增了一个需要被实现的抽象属性:pluginId: String

[KOTLIN] e: file:///Users/forte/IdeaProjects/suspend-transform-kotlin-compile-plugin/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/SuspendTransformComponentRegistrar.kt:38:1 Class 'SuspendTransformComponentRegistrar' is not abstract and does not implement abstract base class member:
val pluginId: String

看样子是打算对一些信息或日志之类的进行更明确更友好的提示了?当然应该也不止于此,我们来看看它的文档注释:

/**
* Uniquely identifies the Kotlin compiler plugin. Must match the `pluginId` specified in [CommandLineProcessor].
* The ID can be used in combination with `-Xcompiler-plugin-order` to control execution order of compiler plugins.
*/
val pluginId: String

好吧,看来还是有些限制的,它的值要求跟 CommandLineProcessor.pluginId 一致,以此可以通过编译器参数来让用户指定编译器插件的执行顺序。

getContainingClassSymbol

接下来就是老生常谈的 API 消失和变化了。从错误日志中可以看到,扩展函数 org.jetbrains.kotlin.fir.analysis.checkers.getContainingClassSymbol 消失了。
要找到它跑到哪里去了,就要先看看它之前是怎么用:

import org.jetbrains.kotlin.fir.analysis.checkers.getContainingClassSymbol// ...val overriddenAnnotation = firAnnotation(overriddenFunction, markAnnotation, overriddenFunction.getContainingClassSymbol()
) ?: return@processOverridden

根据我的经验,通常情况下这类扩展函数不会被真正的_移除_,大多数都是被挪了个地方、改了个名字之类的。修复起来也很简单:借助我们万能的IDE提示,
找到它新的位置:

import org.jetbrains.kotlin.fir.resolve.getContainingClassSymbol// ...val overriddenAnnotation = firAnnotation(overriddenFunction, markAnnotation, overriddenFunction.getContainingClassSymbol()
) ?: return@processOverridden

不出所料,它只是换了个包。😊

symbols -> irBuiltIns

下一个变化就更加慈悲了:它没有一声不吭的离开,而是好心的标记了废弃,并给我留下了温柔的指引。

先来看看代码中是怎么使用的:

val suspendLambda = context.createSuspendLambdaWithCoroutineScope(parent = originFunction.parent,// suspend () -> ?lambdaType = context.symbols.suspendFunctionN(0).typeWith(originFunction.returnType),originFunction = originFunction
).also { +it }

它会构建一个 suspend () -> ? 的Lambda类型,供编译器插件使用。那么这个时候,
我们会得到警告:

'val symbols: Symbols' is deprecated. This API is deprecated. Use `irBuiltIns` instead.

很明显,context.symbols 有更好的替代品:irBuiltIns。事不宜迟,换上看看吧:

val suspendLambda = context.createSuspendLambdaWithCoroutineScope(parent = originFunction.parent,// suspend () -> ?lambdaType = context.irBuiltIns.suspendFunctionN(0).typeWith(originFunction.returnType),originFunction = originFunction
).also { +it }

有趣的是,被废弃的类型 Symbols 其实也是对 IrBuiltIns 的一种包装和扩展。它的定义是:

abstract class Symbols(irBuiltIns: IrBuiltIns) : PreSerializationSymbols.Impl(irBuiltIns) {// ...
}

在构造中,它也是需要一个 IrBuiltIns 的。以其中的 suspendFunctionN(0) 为例,它的实现也是直接使用的
irBuiltIns.suspendFunctionN

总结

老实说,这次更新的变化不是非常大,几个API的变化都很好修复。不过我很喜欢,也希望未来的更新都能这么温柔。

http://www.dtcms.com/a/464903.html

相关文章:

  • go中调用合约
  • 用Python可视化国庆期间旅游概况与消费趋势
  • InitLWIP() 初始化
  • Python爬虫实战:获取新浪旅游热门景点排行榜及数据分析
  • C++设计模式之行为型模式:中介者模式(Mediator)
  • 为什么苏州网络进不了网站ps设计网站
  • [C# starter-kit] Domain Entities | `AuditableEntity`基类 | 跟踪变化 | 软删除
  • 深度复盘+完整源码:我把 libuv 的高性能内存池,用现代 C++ 给你扒了个底朝天
  • GUI 自动化与接口自动化:概念、差异与协同落地
  • 网站建设公司是怎么找客户idc网站模板源码下载
  • kafka的数据消费通过flinksql 入数到Doris的报错(Connection timed out)
  • 【汽车篇】AI深度学习在汽车零部件外观检测——石墨电极板的应用
  • 花型图案设计网站做网站自己能做百度推广吗
  • Java求职面试实战:从Spring Boot到微服务架构的技术探讨
  • 网站做要钱提高美誉度的网络营销方式
  • 自己动手制作鲁大师AiNas手机无线数据线,Termux 安装kod桌面浏览器,打造私人NAS云盘,让电脑和手机零配置无线传输文件
  • 【开题答辩全过程】以 爱心捐赠物资管理系统为例,包含答辩的问题和答案
  • python进阶_Day3
  • 工控机如何联网
  • 温岭市住房和城乡建设局网站云阳如何做网站
  • 大模型应用比赛-表格知识挑战赛总结
  • 通过0x80软件中断执行系统调用
  • (数据库学习四)哈希处理
  • Hibernate中StatelessSession和‌普通Session对比
  • 浙江邮电工程建设有限公司网站企业门户网站建站
  • K8S1.28.2安装与部署
  • 业务知识:强制平仓
  • 币安最高多少杠杆 margin
  • wpf之GroupBox
  • 标签_图片(本文为个人学习笔记,内容整理自哔哩哔哩UP主【非学者勿扰】的公开课程。 > 所有知识点归属原作者,仅作非商业用途分享)