Skip to content

Commit a8873dd

Browse files
authored
Propose rules about main for null-safety (#1146)
Cf. #1120, the rules about `main` impose very few requirements on the signature and the behavior, and the ones that are specified are actually more strict than the implementations. This PR changes the rules such that obviously type-incorrect declarations of `main` are a compile-time error, and additional signatures (with optional arguments) allowed by the tools are now also allowed according to the specification. It is still a requirement that `main` is a method, and no library can declare a `main` which cannot be the initial function. Also, invocations of `main` are now explicitly required to be type safe (which seems to be implemented already). Finally, it allows each tool to reject any `main` declaration according to additional implementation specific constraints, which means that no work is required in any tool in order to support this spec change.
1 parent 2a3c615 commit a8873dd

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

accepted/future-releases/nnbd/feature-specification.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Status: Draft
66

77
## CHANGELOG
88

9+
2020.08.12
10+
- Specify constraints on the `main` function.
11+
912
2020.08.06
1013
- Specify error for uninitialized final instance variable in class
1114
with no generative constructors.
@@ -998,6 +1001,56 @@ These are extended as
9981001
per
9991002
[separate proposal](https://github.com/dart-lang/language/blob/master/resources/type-system/flow-analysis.md).
10001003

1004+
### The main function
1005+
1006+
The section 'Scripts' in the language specification is replaced by the
1007+
following:
1008+
1009+
Let _L_ be a library that exports a declaration _D_ named `main`. It is a
1010+
compile-time error unless _D_ is a function declaration. It is a
1011+
compile-time error if _D_ declares more than two required positional
1012+
parameters, or if there are any required named parameters. It is a
1013+
compile-time error if _D_ declares at least one positional parameter, and
1014+
the first positional parameter has a type which is not a supertype of
1015+
`List<String>`.
1016+
1017+
Implementations are free to impose any additional restrictions on the
1018+
signature of `main`.
1019+
1020+
A _script_ is a library that exports a declaration named `main`.
1021+
A script _L_ is executed as follows:
1022+
1023+
First, _L_ is compiled as a library as specified above.
1024+
Then, the top-level function defined by `main`
1025+
in the exported namespace of _L_ is invoked as follows:
1026+
1027+
If `main` can be called with with two positional arguments,
1028+
it is invoked with the following two actual arguments:
1029+
1030+
- An object whose run-time type implements `List<String>`.
1031+
- An object specified when the current isolate _i_ was created,
1032+
for example through the invocation of `Isolate.spawnUri` that spawned _i_,
1033+
or the null object if no such object was supplied.
1034+
A dynamic error occurs if the run-time type of this object is not a
1035+
subtype of the declared type of the corresponding parameter of `main`.
1036+
1037+
If `main` cannot be called with two positional arguments, but it can be
1038+
called with one positional argument, it is invoked with an object whose
1039+
run-time type implements `List<String>` as the only argument.
1040+
1041+
If `main` cannot be called with one or two positional arguments, it is
1042+
invoked with no arguments.
1043+
1044+
In each of the above three cases, an implementation is free to provide
1045+
additional arguments allowed by the signature of `main` (*the above rules
1046+
ensure that the corresponding parameters are optional*). But the
1047+
implementation must ensure that a dynamic error occurs if an actual
1048+
argument does not have a run-time type which is a subtype of the declared
1049+
type of the parameter.
1050+
1051+
A Dart program will typically be executed by executing a script. The
1052+
procedure whereby this script is chosen is implementation specific.
1053+
10011054
## Runtime semantics
10021055

10031056
### Weak and strong semantics

0 commit comments

Comments
 (0)