Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

K2: a secondary constructor's body is never converted

オープン
#174 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
java, kotlin
領域
compilers

調査の方向性

Start in KotlinScan.finalizeType, where each constructor body is assembled as delegation call, parameter-property assignments, then planned init blocks, and look at how runsInitOf decides which constructor runs the init blocks. Then in maddi-kotlin-k2 find where the primary constructor's body is converted and mirror it for KtSecondaryConstructor.bodyBlockExpression, appending it after the current prefix. ConstructorParameterNamedAsPropertyTest deliberately avoids a secondary constructor with a body, so extend it (or add a sibling test) with the snippet from the issue and check that n reads as assigned. Done means a secondary constructor's own body statements appear in the CST after the delegation call and init blocks.

索引モデルが issue の本文から書いたものです。

説明

The K2 front-end never converts a secondary constructor's block body. Given

class Secondary {
    val n: Int
    constructor(n: Int) { this.n = n + 1 }
}

the constructor's CST body is empty. KotlinScan.finalizeType builds every constructor body as: the explicit this(..)/super(..) invocation, then the parameter-property assignments, then the init blocks planned for it. Nothing in maddi-kotlin-k2 reads KtSecondaryConstructor.bodyBlockExpression. Only the delegation call of a secondary constructor survives.

Consequence: the body's assignments, calls and modifications are invisible to the analysis. Above, n is never assigned. A field assigned only there looks unassigned, and a parameter modified only there looks unmodified. That is the unsafe direction.

What kotlinc does: a secondary constructor runs its delegation call; when that is super(..) (or implicit), it then runs the property initializers and init blocks. Its own body comes last. A body converted after the current prefix and planned init blocks matches that order (runsInitOf already picks the constructor that runs the init blocks).

Found while fixing the same-name parameter assignment (CodeLaser/maddi-mod#73). ConstructorParameterNamedAsPropertyTest avoids a secondary constructor with a body for that reason.

主要言語
Java
スター
1
フォーク
1
平均マージ
1時間 55分
マージ済み PR(30日)
8

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

CodeLaser/maddi のほかの issue

CodeLaser/maddi の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。