Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 3d9c192

Browse files
authored
Enable C# nullable reference types in Compiler project (#648)
* Enable nullable in Compiler.csproj * Update nullables in CompilationLoader.cs * Update nullables in ExternalRewriteSteps.cs * Update nullables in FunctorGeneration.cs * Update nullables in Logging.cs * Fix more build errors * Fix last build error * Add QsCompiler to InternalsVisibleTo
1 parent e1a43f3 commit 3d9c192

20 files changed

+149
-118
lines changed

src/QsCompiler/CommandLineTool/Commands/Build.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public static int Run(BuildOptions options, ConsoleLogger logger)
206206
{
207207
ProjectName = options.ProjectName,
208208
AssemblyConstants = assemblyConstants,
209-
TargetPackageAssemblies = options.TargetSpecificDecompositions,
209+
TargetPackageAssemblies = options.TargetSpecificDecompositions ?? Enumerable.Empty<string>(),
210210
RuntimeCapabilities = options.RuntimeCapabilites,
211211
SkipMonomorphization = options.RuntimeCapabilites == RuntimeCapabilities.Unknown,
212212
GenerateFunctorSupport = true,
@@ -226,7 +226,11 @@ public static int Run(BuildOptions options, ConsoleLogger logger)
226226
CompilationLoader.CompilationTaskEvent += CompilationTracker.OnCompilationTaskEvent;
227227
}
228228

229-
var loaded = new CompilationLoader(options.LoadSourcesOrSnippet(logger), options.References, loadOptions, logger);
229+
var loaded = new CompilationLoader(
230+
options.LoadSourcesOrSnippet(logger),
231+
options.References ?? Enumerable.Empty<string>(),
232+
loadOptions,
233+
logger);
230234
if (options.PerfFolder != null)
231235
{
232236
try

src/QsCompiler/CommandLineTool/Commands/Diagnose.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public static int Run(DiagnoseOptions options, ConsoleLogger logger)
274274
var loadOptions = new CompilationLoader.Configuration
275275
{
276276
AssemblyConstants = assemblyConstants,
277-
TargetPackageAssemblies = options.TargetSpecificDecompositions,
277+
TargetPackageAssemblies = options.TargetSpecificDecompositions ?? Enumerable.Empty<string>(),
278278
RuntimeCapabilities = options.RuntimeCapabilites,
279279
SkipMonomorphization = options.RuntimeCapabilites == RuntimeCapabilities.Unknown,
280280
GenerateFunctorSupport = true,
@@ -285,7 +285,11 @@ public static int Run(DiagnoseOptions options, ConsoleLogger logger)
285285
EnableAdditionalChecks = true,
286286
ExposeReferencesViaTestNames = options.ExposeReferencesViaTestNames
287287
};
288-
var loaded = new CompilationLoader(options.LoadSourcesOrSnippet(logger), options.References, loadOptions, logger);
288+
var loaded = new CompilationLoader(
289+
options.LoadSourcesOrSnippet(logger),
290+
options.References ?? Enumerable.Empty<string>(),
291+
loadOptions,
292+
logger);
289293
if (loaded.VerifiedCompilation == null)
290294
{
291295
return ReturnCode.Status(loaded);

src/QsCompiler/CommandLineTool/Commands/Format.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,18 @@ ImmutableDictionary<Uri, string> LoadSources(SourceFileLoader loadFromDisk) =>
175175
options.LoadSourcesOrSnippet(logger)(loadFromDisk)
176176
.ToImmutableDictionary(entry => entry.Key, entry => UpdateArrayLiterals(entry.Value)); // manually replace array literals
177177

178-
var loaded = new CompilationLoader(LoadSources, options.References, logger: logger); // no rewrite steps, no generation
178+
// no rewrite steps, no generation
179+
var loaded =
180+
new CompilationLoader(LoadSources, options.References ?? Enumerable.Empty<string>(), logger: logger);
179181
if (ReturnCode.Status(loaded) == ReturnCode.UNRESOLVED_FILES)
180182
{
181183
return ReturnCode.UNRESOLVED_FILES; // ignore compilation errors
182184
}
185+
else if (loaded.VerifiedCompilation is null)
186+
{
187+
logger.Log(ErrorCode.QsGenerationFailed, Enumerable.Empty<string>());
188+
return ReturnCode.CODE_GENERATION_ERRORS;
189+
}
183190

184191
// TODO: a lot of the formatting logic defined here and also in the routines above
185192
// is supposed to move into the compilation manager in order to be available for the language server to provide formatting

src/QsCompiler/CommandLineTool/CompilationTracker.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private class CompilationTask
2626
/// <summary>
2727
/// Represents the name of the parent compilation task.
2828
/// </summary>
29-
public readonly string ParentName;
29+
public readonly string? ParentName;
3030

3131
/// <summary>
3232
/// Represents the name of the compilation task.
@@ -56,15 +56,15 @@ private class CompilationTask
5656
/// <summary>
5757
/// Generates a key that uniquely identifies a task in the compilation process based on the task's name and its parent's name.
5858
/// </summary>
59-
internal static string GenerateKey(string parentName, string name)
59+
internal static string GenerateKey(string? parentName, string name)
6060
{
6161
return string.Format("{0}.{1}", parentName ?? "ROOT", name);
6262
}
6363

6464
/// <summary>
6565
/// Creates a compilation task object and starts its stopwatch.
6666
/// </summary>
67-
public CompilationTask(string parentName, string name)
67+
public CompilationTask(string? parentName, string name)
6868
{
6969
this.ParentName = parentName;
7070
this.Name = name;

src/QsCompiler/CommandLineTool/Options.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,17 +298,17 @@ public static bool IsCodeSnippet(NonNullable<string> fileId) =>
298298
/// </summary>
299299
internal CompilationLoader.SourceLoader LoadSourcesOrSnippet(ILogger logger) => loadFromDisk =>
300300
{
301-
bool inputIsEmptyOrNull = this.Input == null || !this.Input.Any();
302-
if (this.CodeSnippet == null && !inputIsEmptyOrNull)
301+
var input = this.Input ?? Enumerable.Empty<string>();
302+
if (this.CodeSnippet == null && input.Any())
303303
{
304-
return loadFromDisk(this.Input);
304+
return loadFromDisk(input);
305305
}
306-
else if (this.CodeSnippet != null && inputIsEmptyOrNull)
306+
else if (this.CodeSnippet != null && !input.Any())
307307
{
308308
return new Dictionary<Uri, string> { { SNIPPET_FILE_URI, AsSnippet(this.CodeSnippet, this.WithinFunction) } }.ToImmutableDictionary();
309309
}
310310

311-
if (inputIsEmptyOrNull)
311+
if (!input.Any())
312312
{
313313
logger?.Log(ErrorCode.MissingInputFileOrSnippet, Enumerable.Empty<string>());
314314
}

src/QsCompiler/CompilationManager/Diagnostics.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ internal static string Warning(int code) =>
129129

130130
// warnings 70**
131131

132-
public static Diagnostic LoadWarning(WarningCode code, IEnumerable<string> args, string source) =>
132+
public static Diagnostic LoadWarning(WarningCode code, IEnumerable<string> args, string? source) =>
133133
new Diagnostic
134134
{
135135
Severity = DiagnosticSeverity.Warning,
@@ -164,7 +164,7 @@ internal static string Error(int code) =>
164164

165165
// errors 70**
166166

167-
public static Diagnostic LoadError(ErrorCode code, IEnumerable<string> args, string source) =>
167+
public static Diagnostic LoadError(ErrorCode code, IEnumerable<string> args, string? source) =>
168168
new Diagnostic
169169
{
170170
Severity = DiagnosticSeverity.Error,

0 commit comments

Comments
 (0)