Files
sdk/tests/language/extension_methods/static_extension_method_context_test.dart
T
Robert Nystrom 3166d7188f Format tests/language/e*.
Since I regenerated the test expectations, this also fills in a few
unspecified errors with more precise information.

Change-Id: I4a2450de030a7d38b08f30f46d9d9feca99404f5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/407082
Reviewed-by: Lasse Nielsen <lrn@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
2025-02-03 03:29:35 -08:00

48 lines
1.3 KiB
Dart

// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// Tests that the type inference for generic extension method invocations
/// properly accounts for the downward inference context.
import 'package:expect/static_type_helper.dart';
extension E on Object? {
T f<T>(List<T> t) => t.first;
}
class C {
T g<T>(List<T> t) => t.first;
}
main() {
var string = '';
context<int>(
string.f(contextType([1])..expectStaticType<Exactly<List<int>>>()),
);
context<int>(
E(string).f(contextType([1])..expectStaticType<Exactly<List<int>>>()),
);
var nullableString = '' as String?;
context<int?>(
nullableString?.f(
contextType([1])..expectStaticType<Exactly<List<int?>>>(),
),
);
context<int?>(
E(
nullableString,
)?.f(contextType([1])..expectStaticType<Exactly<List<int?>>>()),
);
// And just to verify that the expectations above are reasonable, repeat the
// same thing with an ordinary class:
var c = C();
context<int>(c.g(contextType([1])..expectStaticType<Exactly<List<int>>>()));
var nullableC = C() as C?;
context<int?>(
nullableC?.g(contextType([1])..expectStaticType<Exactly<List<int?>>>()),
);
}