Skip to content

[pigeon] Generated Kotlin code has duplicated switch cases for optional members #148065

@bc-lee

Description

@bc-lee

Steps to reproduce

  1. Add pigeon as a dev dependency in your pubspec.yaml file.
  2. Create a dart file which is a input for pigeon generator. It should have a class with several members, where one of them is optional, the other isn't.
  3. Generate Kotlin code using dart run pigeon --input ... --dart_out ... --kotlin_out ....

Expected results

The generated Kotlin code works as expected.

Actual results

The generate Kotlin code has duplicated switch cases and IntelliJ IDEA/Android Studio shows an warning about it.
'when' branch is never reachable / Duplicate label in when

Screenshot 2024-05-10 at 01 12 02

Note that this image is from a different project, but the warning is the same.

After reading generated code, this warning actually doesn't break Dart/Kotlin PlatformChannel communication, as both of members have same type except one is nullable. However, until analyzing the generated code, it is somewhat confusing.

Code sample

I've created a sample repository to reproduce this issue. You can find it here

Below is a part of generated Kotlin code. As you can see, writeValue method has duplicated switch cases for Size type with different type id(129, 130).

  override fun writeValue(stream: ByteArrayOutputStream, value: Any?)   {
    when (value) {
      is Settings -> {
        stream.write(128)
        writeValue(stream, value.toList())
      }
      is Size -> {
        stream.write(129)
        writeValue(stream, value.toList())
      }
      is Size -> {
        stream.write(130)
        writeValue(stream, value.toList())
      }
      else -> super.writeValue(stream, value)
    }
  }

https://github.com/bc-lee/demo-flutter-pigeon-kotlin/blob/2711fa1a95c7ad59865e4c4f682c86bc7abd19db/pigeon_out/android/src/generated/main/kotlin/com/example/pigeon/Bridge.kt#L114-L130

corresponding Dart code is like below.

class Size {
  final int width;
  final int height;

  Size({
    required this.width,
    required this.height,
  });
}

class Settings {
  final bool foo;
  final Size bar;
  final Size? baz;

  Settings({
    required this.foo,
    required this.bar,
    this.baz,
  });
}

https://github.com/bc-lee/demo-flutter-pigeon-kotlin/blob/2711fa1a95c7ad59865e4c4f682c86bc7abd19db/pigeons/bridge.dart

Screenshots or Video

No response

Logs

No response

Flutter Doctor output

Doctor output
$ flutter doctor -v
[✓] Flutter (Channel stable, 3.19.6, on macOS 14.4.1 23E224 darwin-arm64, locale en-KR)
    • Flutter version 3.19.6 on channel stable at /path/to/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 54e66469a9 (3 weeks ago), 2024-04-17 13:08:03 -0700
    • Engine revision c4cd48e186
    • Dart version 3.3.4
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /path/to/android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /path/to/android/sdk
    • Java binary at: /path/to/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode15.2.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (version unknown)
    • Android Studio at /path/to/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Unable to determine Android Studio version.
    • android-studio-dir = /path/to/Applications/Android Studio.app
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)

[✓] IntelliJ IDEA Ultimate Edition (version 2024.1.1)
    • IntelliJ at /path/to/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin installed
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version unknown)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.88.0
    ✗ Unable to determine VS Code version.

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 14.4.1 23E224 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 124.0.6367.119

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listfound in release: 3.19Found to occur in 3.19found in release: 3.22Found to occur in 3.22has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: pigeonrelated to pigeon messaging codegen toolpackageflutter/packages repository. See also p: labels.r: fixedIssue is closed as already fixed in a newer versionteam-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions