Files
sdk/pkg/kernel/test/flatten_test.dart
T
Paul Berry d7f200b97d [kernel] Migrate to new constructor decl syntax.
(Part of https://github.com/dart-lang/sdk/issues/63288)

This change migrates the kernel package to use the new constructor
declaration syntax, described in
https://github.com/dart-lang/language/blob/main/accepted/future-releases/primary-constructors/feature-specification.md#abbreviations-of-in-body-constructor-declarations.

This change was performed in an automated fashion, by (a) bumping the
package's SDK constraint to `3.13.0-0`, (b) enabling the lints
`unnecessary_type_name_in_constructor` and
`unnecessary_const_in_enum_constructor`, (c) fixing the resulting lint
failures using `dart fix`, and then (d) reformatting the affected
files.

To ease code review, I've reverted unrelated formatting changes.

Change-Id: I8d32a0b26450a44dfa2ebd9fe2dff9df6a6a6964
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/508426
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2026-06-01 17:48:34 -07:00

87 lines
2.9 KiB
Dart

// Copyright (c) 2019, 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.
import "package:expect/expect.dart" show Expect;
import 'package:kernel/ast.dart';
import 'package:kernel/class_hierarchy.dart';
import 'package:kernel/testing/type_parser_environment.dart';
import 'package:kernel/type_environment.dart';
const Set<Test> data = {
const Test('Null', 'Null'),
const Test('Never?', 'Never?'),
const Test('void', 'void'),
const Test('dynamic', 'dynamic'),
const Test('bool', 'bool'),
const Test('bool?', 'bool?'),
const Test('List<bool>', 'List<bool>'),
const Test('List<bool>?', 'List<bool>?'),
const Test('FutureOr<bool>', 'bool'),
const Test('FutureOr<bool>?', 'bool?'),
const Test('FutureOr<bool?>?', 'bool?'),
const Test('FutureOr<Null>', 'Null'),
const Test('FutureOr<Null>?', 'Null'),
const Test('Future<bool>', 'bool'),
const Test('Future<bool>?', 'bool?'),
const Test('Future<bool?>', 'bool?'),
const Test('() ->? bool?', '() ->? bool?'),
const Test('() -> bool?', '() -> bool?'),
const Test('() ->? bool', '() ->? bool'),
const Test('() -> bool', '() -> bool'),
const Test('T', 'T', 'T'),
const Test('T?', 'T?', 'T'),
const Test('T', 'T', 'T extends bool'),
const Test('T?', 'T?', 'T extends bool'),
const Test('T', 'bool', 'T extends FutureOr<bool>'),
const Test('T?', 'bool?', 'T extends FutureOr<bool>'),
const Test('T', 'bool', 'T extends Future<bool>'),
const Test('T?', 'bool?', 'T extends Future<bool>'),
const Test('T & bool', 'T', 'T'),
const Test('T & bool?', 'T', 'T'),
};
class Test {
final String input;
final String output;
final String? typeParameters;
const new(this.input, this.output, [this.typeParameters]);
}
void main() {
Env env = new Env('');
ClassHierarchy classHierarchy = new ClassHierarchy(
env.component,
env.coreTypes,
);
TypeEnvironment typeEnvironment = new TypeEnvironment(
env.coreTypes,
classHierarchy,
);
data.forEach((Test test) {
env.withTypeParameters(test.typeParameters, (
List<TypeParameter> typeParameterNodes,
) {
String input = test.input;
String output = test.output;
DartType inputType = env.parseType(input);
DartType expectedOutputType = env.parseType(output);
DartType actualOutputType = typeEnvironment.flatten(inputType);
print(
'flatten($inputType) '
'${test.typeParameters != null ? 'with ${test.typeParameters} ' : ''}'
'= $actualOutputType, expected $expectedOutputType',
);
Expect.equals(
expectedOutputType,
actualOutputType,
"Unexpected flatten of $inputType ('$input'):\n"
"Expected: ${expectedOutputType} ('$output')\n"
"Actual: ${actualOutputType}",
);
});
});
}