[dart:js_interop] Add static error for converted functions that contain named params

Like type parameters, these parameters can't be passed from JS and
such functions already do not work as intended as there is no way
to pass named args to a JS function. These named parameters were
being silently ignored in dart2wasm when creating the function
trampoline.

Change-Id: Iebb890de05f8b242e0542c1ec8f2c0582c5232df
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/368062
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
This commit is contained in:
Srujan Gaddam 2024-06-14 19:42:10 +00:00 committed by Commit Queue
parent b9b4d616a4
commit 06ec78851b
5 changed files with 34 additions and 0 deletions

View file

@ -10508,6 +10508,20 @@ const MessageCode messageJsInteropExternalMemberNotJSAnnotated =
r"""Try removing the 'external' keyword or adding a JS interop annotation.""",
);
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Code<Null> codeJsInteropFunctionToJSNamedParameters =
messageJsInteropFunctionToJSNamedParameters;
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const MessageCode messageJsInteropFunctionToJSNamedParameters =
const MessageCode(
"JsInteropFunctionToJSNamedParameters",
problemMessage:
r"""Functions converted via `toJS` cannot declare named parameters.""",
correctionMessage:
r"""Remove the declared named parameters from the function.""",
);
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Code<Null> codeJsInteropFunctionToJSTypeParameters =
messageJsInteropFunctionToJSTypeParameters;

View file

@ -17,6 +17,7 @@ import 'package:_fe_analyzer_shared/src/messages/codes.dart'
messageJsInteropExternalExtensionMemberOnTypeInvalid,
messageJsInteropExternalExtensionMemberWithStaticDisallowed,
messageJsInteropExternalMemberNotJSAnnotated,
messageJsInteropFunctionToJSNamedParameters,
messageJsInteropFunctionToJSTypeParameters,
messageJsInteropInvalidStaticClassMemberName,
messageJsInteropNamedParameters,
@ -756,6 +757,9 @@ class JsInteropChecks extends RecursiveVisitor {
if (functionType.typeParameters.isNotEmpty) {
report(messageJsInteropFunctionToJSTypeParameters);
}
if (functionType.namedParameters.isNotEmpty) {
report(messageJsInteropFunctionToJSNamedParameters);
}
_reportFunctionToJSInvocationIfNotAllowedFunctionType(functionType, node);
}
}

View file

@ -638,6 +638,8 @@ JsInteropExternalExtensionMemberWithStaticDisallowed/analyzerCode: Fail # Web co
JsInteropExternalExtensionMemberWithStaticDisallowed/example: Fail # Web compiler specific
JsInteropExternalMemberNotJSAnnotated/analyzerCode: Fail # Web compiler specific
JsInteropExternalMemberNotJSAnnotated/example: Fail # Web compiler specific
JsInteropFunctionToJSNamedParameters/analyzerCode: Fail # Web compiler specific
JsInteropFunctionToJSNamedParameters/example: Fail # Web compiler specific
JsInteropFunctionToJSRequiresStaticType/analyzerCode: Fail # Web compiler specific
JsInteropFunctionToJSRequiresStaticType/example: Fail # Web compiler specific
JsInteropFunctionToJSTypeParameters/analyzerCode: Fail # Web compiler specific

View file

@ -5724,6 +5724,10 @@ JsInteropExternalMemberNotJSAnnotated:
problemMessage: "Only JS interop members may be 'external'."
correctionMessage: "Try removing the 'external' keyword or adding a JS interop annotation."
JsInteropFunctionToJSNamedParameters:
problemMessage: "Functions converted via `toJS` cannot declare named parameters."
correctionMessage: "Remove the declared named parameters from the function."
JsInteropFunctionToJSRequiresStaticType:
problemMessage: "`Function.toJS` requires a statically known function type, but Type '#type' is not a precise function type, e.g., `void Function()`."
correctionMessage: "Insert an explicit cast to the expected function type."

View file

@ -240,6 +240,16 @@ void functionToJSTest<T extends JSAny, U extends ExtensionType,
// ^
// [web] Function converted via 'toJS' contains invalid types in its function signature: '*T* Function(*T*)'.
// [web] Functions converted via `toJS` cannot declare type parameters.
(({JSNumber? n}) => n).toJS;
// ^
// [web] Functions converted via `toJS` cannot declare named parameters.
((JSString _, {int n = 0}) => n).toJS;
// ^
// [web] Functions converted via `toJS` cannot declare named parameters.
(({int n = 0, JSArray? a}) => n).toJS;
// ^
// [web] Functions converted via `toJS` cannot declare named parameters.
}
void main() {}