From 75ddc88a4041ef114702fcbfb02b3a0d6bdb2113 Mon Sep 17 00:00:00 2001 From: AlekseyTs Date: Wed, 25 Mar 2020 16:28:26 -0700 Subject: [PATCH] Limit top-level statements to a single compilation unit within a program (#3292) Reflect LDM decision made on 2020-03-09 to limit top-level statements to a single compilation unit within a program. --- proposals/Simple-programs.md | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/proposals/Simple-programs.md b/proposals/Simple-programs.md index fa2aac0..5b098ab 100644 --- a/proposals/Simple-programs.md +++ b/proposals/Simple-programs.md @@ -47,18 +47,16 @@ compilation_unit ; ``` -In all but one *compilation_unit* the *statement*s must all be local function declarations. +Only one *compilation_unit* is allowed to have *statement*s. Example: ``` c# -// File 1 - any statements -if (args.Length == 0 - || !int.TryParse(args[0], out int n) +if (System.Environment.CommandLine.Length == 0 + || !int.TryParse(System.Environment.CommandLine, out int n) || n < 0) return; Console.WriteLine(Fib(n).curr); -// File 2 - only local functions (int curr, int prev) Fib(int i) { if (i == 0) return (1, 0); @@ -78,9 +76,7 @@ static class Program { static async Task Main() { - // File 1 statements - // File 2 local functions - // ... + // statements } } ``` @@ -91,12 +87,7 @@ source code. The method is designated as the entry point of the program. Explicitly declared methods that by convention could be considered as an entry point candidates are ignored. A warning is reported when that happens. It is -an error to specify `-main:` compiler switch. - -If any one compilation unit has statements other than local function declarations, statements from that -compilation unit occur first. This causes it to be legal for local functions in one file to reference -local variables in another. The order of statement contributions (which would all be local functions) -from other compilation units is undefined. +an error to specify `-main:` compiler switch when there are top-level statements. Async operations are allowed in top-level statements to the degree they are allowed in statements within a regular async entry point method. However, they are not required, if `await` expressions and other async @@ -113,13 +104,11 @@ static class $Program { static void $Main() { - // Statements from File 1 - if (args.Length == 0 - || !int.TryParse(args[0], out int n) + if (System.Environment.CommandLine.Length == 0 + || !int.TryParse(System.Environment.CommandLine, out int n) || n < 0) return; Console.WriteLine(Fib(n).curr); - // Local functions from File 2 (int curr, int prev) Fib(int i) { if (i == 0) return (1, 0); @@ -132,7 +121,6 @@ static class $Program At the same time an example like this: ``` c# -// File 1 await System.Threading.Tasks.Task.Delay(1000); System.Console.WriteLine("Hi!"); ``` @@ -143,7 +131,6 @@ static class $Program { static async Task $Main() { - // Statements from File 1 await System.Threading.Tasks.Task.Delay(1000); System.Console.WriteLine("Hi!"); } @@ -153,7 +140,8 @@ static class $Program ### Scope of top-level local variables and local functions Even though top-level local variables and functions are "wrapped" -into the generated entry point method, they should still be in scope throughout the program. +into the generated entry point method, they should still be in scope throughout the program in +every compilation unit. For the purpose of simple-name evaluation, once the global namespace is reached: - First, an attempt is made to evaluate the name within the generated entry point method and only if this attempt fails