Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code excerpts to new type promotion failures #5359

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions examples/non_promotion/lib/non_promotion.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
// ignore_for_file: expected_executable, missing_statement
// ignore_for_file: unused_local_variable, unused_element
// ignore_for_file: prefer_function_declarations_over_variables
// ignore_for_file: prefer_if_null_operators

// #docregion not-field, conflicting-getter
import 'dart:math';
// #enddocregion not-field, conflicting-getter

// #docregion mock
import 'package:mockito/mockito.dart';
// #enddocregion mock

class C1 {
int? i;
Expand Down Expand Up @@ -242,3 +251,137 @@ void miscDeclAnalyzedButNotTested() {
// #enddocregion closure-write-capture
}
}

// #docregion this
extension on int? {
int get valueOrZero {
final self = this;
return self == null ? 0 : self;
}
}
// #enddocregion this

// #docregion private
class PrivateFieldExample {
final int? _val;
PrivateFieldExample(this._val);
}

void test(PrivateFieldExample x) {
if (x._val != null) {
print(x._val + 1);
}
}
// #enddocregion private

// #docregion final
class FinalExample {
final int? _immutablePrivateField;
FinalExample(this._immutablePrivateField);

void f() {
if (_immutablePrivateField != null) {
int i = _immutablePrivateField; // OK
}
}
}
// #enddocregion final

// #docregion not-field
abstract class NotFieldExample {
int? get _value => Random().nextBool() ? 123 : null;
}

void f(NotFieldExample x) {
final value = x._value;
if (value != null) {
print(value.isEven); // OK
}
}
// #enddocregion not-field

// #docregion external
class ExternalExample {
external final int? _externalField;

void f() {
final i = _externalField;
if (i != null) {
print(i.isEven); // OK
}
}
}
// #enddocregion external

// #docregion conflicting-getter
class GetterExample {
final int? _overridden;
GetterExample(this._overridden);
}

class Override implements GetterExample {
@override
int? get _overridden => Random().nextBool() ? 1 : null;
}

void testParity(GetterExample x) {
final i = x._overridden;
if (i != null) {
print(i.isEven); // OK
}
}
// #enddocregion conflicting-getter

// #docregion unrelated
class UnrelatedExample {
final int? _i;
UnrelatedExample(this._i);
}

class Unrelated {
int? get _j => Random().nextBool() ? 1 : null;
}

void f2(UnrelatedExample x) {
if (x._i != null) {
int i = x._i; // OK
}
}
// #enddocregion unrelated

// #docregion conflicting-field
class FieldExample {
final int? _overridden;
FieldExample(this._overridden);
}

class Override2 implements FieldExample {
@override
int? _overridden;
}

void f3(FieldExample x) {
final i = x._overridden;
if (i != null) {
print(i.isEven); // OK
}
}
// #enddocregion conflicting-field

// #docregion mock
class MockingExample {
final int? _i;
MockingExample(this._i);
}

class MockExample extends Mock implements MockingExample {
@override
late final int? _i;
}

void f4(MockingExample x) {
if (x._i != null) {
int i = x._i; // OK
}
}
// #enddocregion mock
3 changes: 3 additions & 0 deletions examples/non_promotion/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ version: 0.0.1
environment:
sdk: ^3.4.0

dependencies:
mockito: ^5.4.4

dev_dependencies:
lints: ^4.0.0
Loading