Skip to content

Commit ced2d0e

Browse files
author
John Messerly
committed
Revert "fix #25794, infer parameter type from default value"
It caused a regression in cyclic_default_values_test in DDC. Fixes #26785 This reverts commit d43f687. [email protected] Review URL: https://codereview.chromium.org/2109353004 .
1 parent 5026d03 commit ced2d0e

File tree

6 files changed

+34
-77
lines changed

6 files changed

+34
-77
lines changed

pkg/analyzer/lib/src/generated/resolver.dart

+3-20
Original file line numberDiff line numberDiff line change
@@ -4857,7 +4857,7 @@ class InferenceContext {
48574857
* Place an info node into the error stream indicating that a
48584858
* [type] has been inferred as the type of [node].
48594859
*/
4860-
void recordInference(AstNode node, DartType type) {
4860+
void recordInference(Expression node, DartType type) {
48614861
if (!_inferenceHints) {
48624862
return;
48634863
}
@@ -5652,11 +5652,6 @@ class ResolverVisitor extends ScopedVisitor {
56525652
*/
56535653
bool resolveOnlyCommentInFunctionBody = false;
56545654

5655-
/**
5656-
* True if we're analyzing in strong mode.
5657-
*/
5658-
bool _strongMode;
5659-
56605655
/**
56615656
* Body of the function currently being analyzed, if any.
56625657
*/
@@ -5688,7 +5683,6 @@ class ResolverVisitor extends ScopedVisitor {
56885683
this.typeSystem = definingLibrary.context.typeSystem;
56895684
bool strongModeHints = false;
56905685
AnalysisOptions options = definingLibrary.context.analysisOptions;
5691-
_strongMode = options.strongMode;
56925686
if (options is AnalysisOptionsImpl) {
56935687
strongModeHints = options.strongModeHints;
56945688
}
@@ -6379,24 +6373,13 @@ class ResolverVisitor extends ScopedVisitor {
63796373

63806374
@override
63816375
Object visitDefaultFormalParameter(DefaultFormalParameter node) {
6382-
ParameterElement element = node.element;
6383-
InferenceContext.setType(node.defaultValue, element.type);
6376+
InferenceContext.setType(node.defaultValue, node.parameter.element?.type);
63846377
super.visitDefaultFormalParameter(node);
6378+
ParameterElement element = node.element;
63856379
if (element.initializer != null && node.defaultValue != null) {
63866380
(element.initializer as FunctionElementImpl).returnType =
63876381
node.defaultValue.staticType;
63886382
}
6389-
if (_strongMode &&
6390-
node.defaultValue != null &&
6391-
element.hasImplicitType &&
6392-
element is! FieldFormalParameterElement) {
6393-
6394-
DartType type = node.defaultValue.staticType;
6395-
if (!type.isBottom && !type.isDynamic) {
6396-
(element as ParameterElementImpl).type = type;
6397-
inferenceContext.recordInference(node, type);
6398-
}
6399-
}
64006383
// Clone the ASTs for default formal parameters, so that we can use them
64016384
// during constant evaluation.
64026385
if (!LibraryElementImpl.hasResolutionCapability(

pkg/analyzer/lib/src/summary/summarize_elements.dart

+2-6
Original file line numberDiff line numberDiff line change
@@ -902,14 +902,10 @@ class _CompilationUnitSerializer {
902902
DartType type = parameter.type;
903903
if (parameter.hasImplicitType) {
904904
Element contextParent = context.enclosingElement;
905-
// Strong mode infers parameters in two cases:
906-
// - instance members (i.e. not constructors or static members),
907-
// - parameters with default values, except initializing formals
908-
// (the type comes from the field).
909905
if (!parameter.isInitializingFormal &&
910906
contextParent is ExecutableElement &&
911-
(!contextParent.isStatic && contextParent is! ConstructorElement ||
912-
parameter.parameterKind != ParameterKind.REQUIRED)) {
907+
!contextParent.isStatic &&
908+
contextParent is! ConstructorElement) {
913909
b.inferredTypeSlot = storeInferredType(type, context);
914910
}
915911
} else {

pkg/analyzer/test/src/summary/resynthesize_ast_test.dart

-6
Original file line numberDiff line numberDiff line change
@@ -508,12 +508,6 @@ var b = a.m();
508508
super.test_inferCorrectlyOnMultipleVariablesDeclaredTogether();
509509
}
510510

511-
@override
512-
@failingTest
513-
void test_inferDefaultFormalParameter() {
514-
super.test_inferDefaultFormalParameter();
515-
}
516-
517511
@override
518512
@failingTest
519513
void test_inferenceInCyclesIsDeterministic() {

pkg/analyzer/test/src/summary/resynthesize_test.dart

+22-15
Original file line numberDiff line numberDiff line change
@@ -4107,7 +4107,9 @@ void f<T, U>() {
41074107
checkLibrary('class C { bool operator<=(C other) => false; }');
41084108
}
41094109

4110-
test_parameterType_inferred_constructor() {
4110+
test_parameterTypeNotInferred_constructor() {
4111+
// Strong mode doesn't do type inference on constructor parameters, so it's
4112+
// ok that we don't store inferred type info for them in summaries.
41114113
checkLibrary('''
41124114
class C {
41134115
C.positional([x = 1]);
@@ -4116,31 +4118,36 @@ class C {
41164118
''');
41174119
}
41184120

4119-
test_parameterType_inferred_staticMethod() {
4121+
test_parameterTypeNotInferred_initializingFormal() {
4122+
// Strong mode doesn't do type inference on initializing formals, so it's
4123+
// ok that we don't store inferred type info for them in summaries.
41204124
checkLibrary('''
41214125
class C {
4122-
static void positional([x = 1]) {}
4123-
static void named({x: 1}) {}
4126+
var x;
4127+
C.positional([this.x = 1]);
4128+
C.named({this.x: 1});
41244129
}
41254130
''');
41264131
}
41274132

4128-
test_parameterType_inferred_topLevelFunction() {
4133+
test_parameterTypeNotInferred_staticMethod() {
4134+
// Strong mode doesn't do type inference on parameters of static methods,
4135+
// so it's ok that we don't store inferred type info for them in summaries.
41294136
checkLibrary('''
4130-
void positional([x = 1]) {}
4131-
void named({x: 1}) {}
4137+
class C {
4138+
static void positional([x = 1]) {}
4139+
static void named({x: 1}) {}
4140+
}
41324141
''');
41334142
}
41344143

4135-
test_parameterTypeNotInferred_initializingFormal() {
4136-
// Strong mode doesn't do type inference on initializing formals, so it's
4137-
// ok that we don't store inferred type info for them in summaries.
4144+
test_parameterTypeNotInferred_topLevelFunction() {
4145+
// Strong mode doesn't do type inference on parameters of top level
4146+
// functions, so it's ok that we don't store inferred type info for them in
4147+
// summaries.
41384148
checkLibrary('''
4139-
class C {
4140-
var x;
4141-
C.positional([this.x = 1]);
4142-
C.named({this.x: 1});
4143-
}
4149+
void positional([x = 1]) {}
4150+
void named({x: 1}) {}
41444151
''');
41454152
}
41464153

pkg/analyzer/test/src/task/strong/checker_test.dart

+6-2
Original file line numberDiff line numberDiff line change
@@ -2037,12 +2037,16 @@ void f1(dynamic x) {}
20372037
// default formal
20382038
void df0([/*error:IMPLICIT_DYNAMIC_PARAMETER*/x = DYNAMIC_VALUE]) {}
20392039
void df1([dynamic x = DYNAMIC_VALUE]) {}
2040-
void df2([/*info:INFERRED_TYPE*/x = 42]) {}
2040+
2041+
// https://github.com/dart-lang/sdk/issues/25794
2042+
void df2([/*error:IMPLICIT_DYNAMIC_PARAMETER*/x = 42]) {}
20412043
20422044
// default formal (named)
20432045
void nf0({/*error:IMPLICIT_DYNAMIC_PARAMETER*/x: DYNAMIC_VALUE}) {}
20442046
void nf1({dynamic x: DYNAMIC_VALUE}) {}
2045-
void nf2({/*info:INFERRED_TYPE*/x: 42}) {}
2047+
2048+
// https://github.com/dart-lang/sdk/issues/25794
2049+
void nf2({/*error:IMPLICIT_DYNAMIC_PARAMETER*/x: 42}) {}
20462050
20472051
// field formal
20482052
class C {

pkg/analyzer/test/src/task/strong/inferred_type_test.dart

+1-28
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ class Foo {
723723
}
724724
void f([List<int> l = /*info:INFERRED_TYPE_LITERAL*/const [1]]) {}
725725
// We do this inference in an early task but don't preserve the infos.
726-
Function2<List<int>, String> g = /*pass should be info:INFERRED_TYPE_CLOSURE*/([/*info:INFERRED_TYPE*/llll = /*info:INFERRED_TYPE_LITERAL*/const [1]]) => "hello";
726+
Function2<List<int>, String> g = /*pass should be info:INFERRED_TYPE_CLOSURE*/([llll = /*info:INFERRED_TYPE_LITERAL*/const [1]]) => "hello";
727727
''');
728728
}
729729

@@ -1912,33 +1912,6 @@ foo() {
19121912
''');
19131913
}
19141914

1915-
void test_inferDefaultFormalParameter() {
1916-
var unit = checkFile('''
1917-
f([/*info:INFERRED_TYPE*/x = 42]) {}
1918-
g({/*info:INFERRED_TYPE*/x: 'hi'}) {}
1919-
''');
1920-
expect(unit.functions[0].parameters[0].type.toString(), 'int');
1921-
expect(unit.functions[1].parameters[0].type.toString(), 'String');
1922-
}
1923-
1924-
void test_inferDefaultFormalParameter_fieldFormal() {
1925-
checkFile('''
1926-
class C {
1927-
int x;
1928-
var y;
1929-
C({this.x: /*error:INVALID_ASSIGNMENT*/0.0, this.y: 'hi'}) {
1930-
String z = /*info:DYNAMIC_CAST*/y;
1931-
}
1932-
C.c([this.x =/*error:INVALID_ASSIGNMENT*/0.0, this.y = 'hi']) {
1933-
String z = /*info:DYNAMIC_CAST*/y;
1934-
}
1935-
m() {
1936-
String z = /*info:DYNAMIC_CAST*/y;
1937-
}
1938-
}
1939-
''');
1940-
}
1941-
19421915
void test_inferedType_usesSyntheticFunctionType() {
19431916
var mainUnit = checkFile('''
19441917
int f() => null;

0 commit comments

Comments
 (0)