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.24k stars 1.57k forks source link

Incomplete substitution for FunctionType type parameter bounds #38438

Open scheglov opened 5 years ago

scheglov commented 5 years ago

The test below fails, because U is not replaced with a fresh TypeParameter U (new) in the T (new) bounds.

Or is it expected behavior?

import 'package:kernel/kernel.dart';
import 'package:kernel/type_algebra.dart';
import 'package:test/test.dart';

main() {
  test('me', () {
    var Z = TypeParameter('Z');
    var classPair = Class(name: 'int', typeParameters: [
      TypeParameter('T1'),
      TypeParameter('U1'),
      Z,
    ]);

    var T = TypeParameter('T');
    var U = TypeParameter('U');
    T.bound = InterfaceType(classPair, [
      TypeParameterType(T),
      TypeParameterType(U),
      TypeParameterType(Z),
    ]);
    U.bound = InterfaceType(Class(name: 'Object'));

    var ft = FunctionType(
      [TypeParameterType(T), TypeParameterType(U)],
      TypeParameterType(T),
      typeParameters: [T, U],
    );

    var intType = Class(name: 'int').rawType;
    var ft2 = Substitution.fromPairs(
      [Z],
      [intType],
    ).substituteType(ft) as FunctionType;
    print(ft2);

    var T2 = ft2.typeParameters[0];
    var U2 = ft2.typeParameters[1];
    expect(T2, isNot(same(T)));
    expect(U2, isNot(same(U)));
    var boundT = T2.bound as InterfaceType;
    expect((boundT.typeArguments[0] as TypeParameterType).parameter, same(T2));
    expect((boundT.typeArguments[1] as TypeParameterType).parameter, same(U2));
  });
}
derolf commented 5 years ago

Is this related to https://github.com/dart-lang/sdk/issues/38585?

scheglov commented 5 years ago

@derolf I don't think so. This issue is for kernel and CFE, and the one you references is for analyzer.

derolf commented 5 years ago

@scheglov Bug #38585 is caused by wrong substation in visitTypeParameterType, which I fixed in PR https://github.com/dart-lang/sdk/pull/38614