<commit_before>// Copyright (c) 2022, 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 when the feature is enabled, inferred types can flow
// "horizontally" from a non-closure argument of an invocation to a closure
// argument.

// SharedOptions=--enable-experiment=inference-update-1

import '../static_type_helper.dart';

testLaterUnnamedParameter(void Function<T>(T, void Function(T)) f) {
  f(0, (x) {
    x.expectStaticType<Exactly<int>>();
  });
}

testEarlierUnnamedParameter(void Function<T>(void Function(T), T) f) {
  f((x) {
    x.expectStaticType<Exactly<int>>();
  }, 0);
}

testLaterNamedParameter(
    void Function<T>({required T a, required void Function(T) b}) f) {
  f(
      a: 0,
      b: (x) {
        x.expectStaticType<Exactly<int>>();
      });
}

testEarlierNamedParameter(
    void Function<T>({required void Function(T) a, required T b}) f) {
  f(
      a: (x) {
        x.expectStaticType<Exactly<int>>();
      },
      b: 0);
}

testPropagateToReturnType(U Function<T, U>(T, U Function(T)) f) {
  f(0, (x) => [x]).expectStaticType<Exactly<List<int>>>();
}

testFold(List<int> list) {
  var a = list.fold(
      0,
      (x, y) =>
          (x..expectStaticType<Exactly<int>>()) +
          (y..expectStaticType<Exactly<int>>()));
  a.expectStaticType<Exactly<int>>();
}

// The test cases below exercise situations where there are multiple closures in
// the invocation, and they need to be inferred in the right order.

testClosureAsParameterType(U Function<T, U>(T, U Function(T)) f) {
  f(() => 0, (h) => [h()]..expectStaticType<Exactly<List<int>>>())
      .expectStaticType<Exactly<List<int>>>();
}

testPropagateToEarlierClosure(U Function<T, U>(U Function(T), T Function()) f) {
  f((x) => [x]..expectStaticType<Exactly<List<int>>>(), () => 0)
      .expectStaticType<Exactly<List<int>>>();
}

testPropagateToLaterClosure(U Function<T, U>(T Function(), U Function(T)) f) {
  f(() => 0, (x) => [x]..expectStaticType<Exactly<List<int>>>())
      .expectStaticType<Exactly<List<int>>>();
}

testLongDepedencyChain(
    V Function<T, U, V>(T Function(), U Function(T), V Function(U)) f) {
  f(() => [0], (x) => x.single..expectStaticType<Exactly<int>>(),
          (y) => {y}..expectStaticType<Exactly<Set<int>>>())
      .expectStaticType<Exactly<Set<int>>>();
}

testDependencyCycle(Map<T, U> Function<T, U>(T Function(U), U Function(T)) f) {
  f((x) => [x]..expectStaticType<Exactly<List<Object?>>>(),
          (y) => {y}..expectStaticType<Exactly<Set<Object?>>>())
      .expectStaticType<Exactly<Map<List<Object?>, Set<Object?>>>>();
}

testNecessaryDueToWrongExplicitParameterType(List<int> list) {
  var a = list.fold(
      0,
      (x, int y) =>
          (x..expectStaticType<Exactly<int>>()) +
          (y..expectStaticType<Exactly<int>>()));
  a.expectStaticType<Exactly<int>>();
}

testPropagateFromContravariantReturnType(
    U Function<T, U>(void Function(T) Function(), U Function(T)) f) {
  f(() => (int i) {}, (x) => [x]..expectStaticType<Exactly<List<int>>>())
      .expectStaticType<Exactly<List<int>>>();
}

testPropagateToContravariantParameterType(
    U Function<T, U>(T Function(), U Function(void Function(T))) f) {
  f(() => 0, (x) => [x]..expectStaticType<Exactly<List<void Function(int)>>>())
      .expectStaticType<Exactly<List<void Function(int)>>>();
}

testReturnTypeRefersToMultipleTypeVars(
    void Function<T, U>(
            Map<T, U> Function(), void Function(T), void Function(U))
        f) {
  f(() => {0: ''}, (k) {
    k.expectStaticType<Exactly<int>>();
  }, (v) {
    v.expectStaticType<Exactly<String>>();
  });
}

testUnnecessaryDueToNoDependency(T Function<T>(T Function(), T) f) {
  f(() => 0, null).expectStaticType<Exactly<int?>>();
}

testUnnecessaryDueToExplicitParameterType(List<int> list) {
  var a = list.fold(null, (int? x, y) => (x ?? 0) + y);
  a.expectStaticType<Exactly<int?>>();
}

testUnnecessaryDueToExplicitParameterTypeNamed(
    T Function<T>(T, T Function({required T x, required int y})) f) {
  var a = f(null, ({int? x, required y}) => (x ?? 0) + y);
  a.expectStaticType<Exactly<int?>>();
}

testParenthesized(void Function<T>(T, void Function(T)) f) {
  f(0, ((x) {
    x.expectStaticType<Exactly<int>>();
  }));
}

testParenthesizedNamed(
    void Function<T>({required T a, required void Function(T) b}) f) {
  f(
      a: 0,
      b: ((x) {
        x.expectStaticType<Exactly<int>>();
      }));
}

testParenthesizedTwice(void Function<T>(T, void Function(T)) f) {
  f(0, (((x) {
    x.expectStaticType<Exactly<int>>();
  })));
}

testParenthesizedTwiceNamed(
    void Function<T>({required T a, required void Function(T) b}) f) {
  f(
      a: 0,
      b: (((x) {
        x.expectStaticType<Exactly<int>>();
      })));
}

main() {}
<commit_msg>Fix spelling<commit_after>// Copyright (c) 2022, 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 when the feature is enabled, inferred types can flow
// "horizontally" from a non-closure argument of an invocation to a closure
// argument.

// SharedOptions=--enable-experiment=inference-update-1

import '../static_type_helper.dart';

testLaterUnnamedParameter(void Function<T>(T, void Function(T)) f) {
  f(0, (x) {
    x.expectStaticType<Exactly<int>>();
  });
}

testEarlierUnnamedParameter(void Function<T>(void Function(T), T) f) {
  f((x) {
    x.expectStaticType<Exactly<int>>();
  }, 0);
}

testLaterNamedParameter(
    void Function<T>({required T a, required void Function(T) b}) f) {
  f(
      a: 0,
      b: (x) {
        x.expectStaticType<Exactly<int>>();
      });
}

testEarlierNamedParameter(
    void Function<T>({required void Function(T) a, required T b}) f) {
  f(
      a: (x) {
        x.expectStaticType<Exactly<int>>();
      },
      b: 0);
}

testPropagateToReturnType(U Function<T, U>(T, U Function(T)) f) {
  f(0, (x) => [x]).expectStaticType<Exactly<List<int>>>();
}

testFold(List<int> list) {
  var a = list.fold(
      0,
      (x, y) =>
          (x..expectStaticType<Exactly<int>>()) +
          (y..expectStaticType<Exactly<int>>()));
  a.expectStaticType<Exactly<int>>();
}

// The test cases below exercise situations where there are multiple closures in
// the invocation, and they need to be inferred in the right order.

testClosureAsParameterType(U Function<T, U>(T, U Function(T)) f) {
  f(() => 0, (h) => [h()]..expectStaticType<Exactly<List<int>>>())
      .expectStaticType<Exactly<List<int>>>();
}

testPropagateToEarlierClosure(U Function<T, U>(U Function(T), T Function()) f) {
  f((x) => [x]..expectStaticType<Exactly<List<int>>>(), () => 0)
      .expectStaticType<Exactly<List<int>>>();
}

testPropagateToLaterClosure(U Function<T, U>(T Function(), U Function(T)) f) {
  f(() => 0, (x) => [x]..expectStaticType<Exactly<List<int>>>())
      .expectStaticType<Exactly<List<int>>>();
}

testLongDependencyChain(
    V Function<T, U, V>(T Function(), U Function(T), V Function(U)) f) {
  f(() => [0], (x) => x.single..expectStaticType<Exactly<int>>(),
          (y) => {y}..expectStaticType<Exactly<Set<int>>>())
      .expectStaticType<Exactly<Set<int>>>();
}

testDependencyCycle(Map<T, U> Function<T, U>(T Function(U), U Function(T)) f) {
  f((x) => [x]..expectStaticType<Exactly<List<Object?>>>(),
          (y) => {y}..expectStaticType<Exactly<Set<Object?>>>())
      .expectStaticType<Exactly<Map<List<Object?>, Set<Object?>>>>();
}

testNecessaryDueToWrongExplicitParameterType(List<int> list) {
  var a = list.fold(
      0,
      (x, int y) =>
          (x..expectStaticType<Exactly<int>>()) +
          (y..expectStaticType<Exactly<int>>()));
  a.expectStaticType<Exactly<int>>();
}

testPropagateFromContravariantReturnType(
    U Function<T, U>(void Function(T) Function(), U Function(T)) f) {
  f(() => (int i) {}, (x) => [x]..expectStaticType<Exactly<List<int>>>())
      .expectStaticType<Exactly<List<int>>>();
}

testPropagateToContravariantParameterType(
    U Function<T, U>(T Function(), U Function(void Function(T))) f) {
  f(() => 0, (x) => [x]..expectStaticType<Exactly<List<void Function(int)>>>())
      .expectStaticType<Exactly<List<void Function(int)>>>();
}

testReturnTypeRefersToMultipleTypeVars(
    void Function<T, U>(
            Map<T, U> Function(), void Function(T), void Function(U))
        f) {
  f(() => {0: ''}, (k) {
    k.expectStaticType<Exactly<int>>();
  }, (v) {
    v.expectStaticType<Exactly<String>>();
  });
}

testUnnecessaryDueToNoDependency(T Function<T>(T Function(), T) f) {
  f(() => 0, null).expectStaticType<Exactly<int?>>();
}

testUnnecessaryDueToExplicitParameterType(List<int> list) {
  var a = list.fold(null, (int? x, y) => (x ?? 0) + y);
  a.expectStaticType<Exactly<int?>>();
}

testUnnecessaryDueToExplicitParameterTypeNamed(
    T Function<T>(T, T Function({required T x, required int y})) f) {
  var a = f(null, ({int? x, required y}) => (x ?? 0) + y);
  a.expectStaticType<Exactly<int?>>();
}

testParenthesized(void Function<T>(T, void Function(T)) f) {
  f(0, ((x) {
    x.expectStaticType<Exactly<int>>();
  }));
}

testParenthesizedNamed(
    void Function<T>({required T a, required void Function(T) b}) f) {
  f(
      a: 0,
      b: ((x) {
        x.expectStaticType<Exactly<int>>();
      }));
}

testParenthesizedTwice(void Function<T>(T, void Function(T)) f) {
  f(0, (((x) {
    x.expectStaticType<Exactly<int>>();
  })));
}

testParenthesizedTwiceNamed(
    void Function<T>({required T a, required void Function(T) b}) f) {
  f(
      a: 0,
      b: (((x) {
        x.expectStaticType<Exactly<int>>();
      })));
}

main() {}

{'fraction_non_alphanumeric': 0.1958553433563592, 'fraction_numerical': 0.0050792360828931325, 'mean_word_length': 4.064300411522634, 'pattern_counts': {'":': 0, '<': 231, '<?xml version=': 0, '>': 275, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '535066', 'n_tokens_mistral': 3591, 'n_tokens_neox': 3443, 'n_words': 747}