From 2d6d51df998b74ca295bb34153aec64e3d50cbbc Mon Sep 17 00:00:00 2001 From: Erik Ernst Date: Wed, 12 Aug 2020 12:00:57 +0200 Subject: [PATCH 1/2] Propose rules about `main` for null-safety --- .../nnbd/feature-specification.md | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/accepted/future-releases/nnbd/feature-specification.md b/accepted/future-releases/nnbd/feature-specification.md index 37ab596861..4724e30cf7 100644 --- a/accepted/future-releases/nnbd/feature-specification.md +++ b/accepted/future-releases/nnbd/feature-specification.md @@ -6,6 +6,9 @@ Status: Draft ## CHANGELOG +2020.08.12 + - Specify constraints on the `main` function. + 2020.08.06 - Specify error for uninitialized final instance variable in class with no generative constructors. @@ -901,6 +904,53 @@ These are extended as per [separate proposal](https://github.com/dart-lang/language/blob/master/resources/type-system/flow-analysis.md). +### The main function + +The section 'Scripts' in the language specification is replaced by the +following: + +Let _L_ be a library that exports a declaration _D_ named `main`. It is a +compile-time error unless _D_ is a function. It is a compile-time error if +_D_ declares more than two required positional parameters, or if there are +any required named parameters. It is a compile-time error if _D_ declares +at least one positional parameter, and the first positional parameter has a +type which is not a supertype of `List`. + +Implementations are free to impose any additional restrictions on the +signature of `main`. + +A _script_ is a library that exports a declaration named `main`. +A script _L_ is executed as follows: + +First, _L_ is compiled as a library as specified above. +Then, the top-level function defined by `main` +in the exported namespace of _L_ is invoked as follows: + +If `main` can be called with with two positional arguments, +it is invoked with the following two actual arguments: + +- An object whose run-time type implements `List`. +- An object specified when the current isolate _i_ was created, + for example through the invocation of `Isolate.spawnUri` that spawned _i_, + or the null object if no such object was supplied. + A dynamic error occurs unless the run-time type of this object is a + subtype of the declared type of the corresponding parameter of `main`. + +If `main` cannot be called with two positional arguments, but it can be +called with one positional argument, it is invoked with an object whose +run-time type implements `List` as the only argument. + +If `main` cannot be called with one or two positional arguments, it is +invoked with no arguments. + +In each of the above three cases, an implementation is free to provide +additional arguments allowed by the signature of `main`, but must ensure +that a dynamic error occurs if an actual argument does not have a run-time +type which is a subtype of the declared type of the parameter. + +A Dart program will typically be executed by executing a script. +The procedure whereby this script is chosen is implementation specific. + ## Runtime semantics ### Weak and strong semantics From 3a585e5ad96539a3f651a52d73e1d81fcc41af2c Mon Sep 17 00:00:00 2001 From: Erik Ernst Date: Mon, 17 Aug 2020 11:49:30 +0200 Subject: [PATCH 2/2] Review response --- .../nnbd/feature-specification.md | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/accepted/future-releases/nnbd/feature-specification.md b/accepted/future-releases/nnbd/feature-specification.md index 4724e30cf7..0356effaab 100644 --- a/accepted/future-releases/nnbd/feature-specification.md +++ b/accepted/future-releases/nnbd/feature-specification.md @@ -910,11 +910,12 @@ The section 'Scripts' in the language specification is replaced by the following: Let _L_ be a library that exports a declaration _D_ named `main`. It is a -compile-time error unless _D_ is a function. It is a compile-time error if -_D_ declares more than two required positional parameters, or if there are -any required named parameters. It is a compile-time error if _D_ declares -at least one positional parameter, and the first positional parameter has a -type which is not a supertype of `List`. +compile-time error unless _D_ is a function declaration. It is a +compile-time error if _D_ declares more than two required positional +parameters, or if there are any required named parameters. It is a +compile-time error if _D_ declares at least one positional parameter, and +the first positional parameter has a type which is not a supertype of +`List`. Implementations are free to impose any additional restrictions on the signature of `main`. @@ -933,7 +934,7 @@ it is invoked with the following two actual arguments: - An object specified when the current isolate _i_ was created, for example through the invocation of `Isolate.spawnUri` that spawned _i_, or the null object if no such object was supplied. - A dynamic error occurs unless the run-time type of this object is a + A dynamic error occurs if the run-time type of this object is not a subtype of the declared type of the corresponding parameter of `main`. If `main` cannot be called with two positional arguments, but it can be @@ -944,12 +945,14 @@ If `main` cannot be called with one or two positional arguments, it is invoked with no arguments. In each of the above three cases, an implementation is free to provide -additional arguments allowed by the signature of `main`, but must ensure -that a dynamic error occurs if an actual argument does not have a run-time -type which is a subtype of the declared type of the parameter. - -A Dart program will typically be executed by executing a script. -The procedure whereby this script is chosen is implementation specific. +additional arguments allowed by the signature of `main` (*the above rules +ensure that the corresponding parameters are optional*). But the +implementation must ensure that a dynamic error occurs if an actual +argument does not have a run-time type which is a subtype of the declared +type of the parameter. + +A Dart program will typically be executed by executing a script. The +procedure whereby this script is chosen is implementation specific. ## Runtime semantics