dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.57k forks source link

Expression evaluation on extension getters for nullable types throw exception #57040

Open FMorschel opened 2 hours ago

FMorschel commented 2 hours ago

Repro

extension on String? {
  bool get isNullOrEmpty {
    var str = this;
    return str == null || str.isEmpty;
  }
}

void main() {
  String? str;
  print(str.isNullOrEmpty);
}

Place a breakpoint on print and evaluate str.isNullOrEmpty you get:

Unhandled exception:
NoSuchMethodError: The getter 'isNullOrEmpty' was called on null.
Receiver: null
Tried calling: isNullOrEmpty
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5)
#1      Eval ()
#2      main (package:bug/a.dart:10:13)
#3      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#4      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

CC: @bkonyi

dart-github-bot commented 2 hours ago

Summary: The issue is that evaluating an extension getter on a nullable type that is null throws a NoSuchMethodError instead of returning the expected value. This occurs when using a breakpoint in the debugger to evaluate the expression.