Skip to content

Commit

Permalink
fix build warnings
Browse files Browse the repository at this point in the history
This will likely cause new failures, as I turned on nullable annotations and warnings in one of the templates.

Update code samples for the template in use to remove nullable warnings.

I did this as we plan to have nullable annotations on by default in this version of the spec.
  • Loading branch information
BillWagner committed May 9, 2024
1 parent 4860c9b commit dcb3731
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion standard/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ The attribute instance represented by `T`, `C`, `P`, and `N`, and associated wi
<!-- markdownlint-enable MD028 -->
> *Example*: In an implementation of the CLI, the `Help` attribute instances in the assembly created by compiling the example program in [§22.2.3](attributes.md#2223-positional-and-named-parameters) can be retrieved with the following program:
>
> <!-- Example: {template:"standalone-console", name:"RuntimeAttributeInstanceRetrieval", expectedOutput:["Type : HelpAttribute","Type : InterrogateHelpUrls"], additionalFiles:["HelpAttribute.cs"], executionArgs:["RuntimeAttributeInstanceRetrieval"]} -->
> <!-- Example: {template:"standalone-console", name:"RuntimeAttributeInstanceRetrieval", expectedOutput:["Type : Microsoft.CodeAnalysis.EmbeddedAttribute", "Type : System.Runtime.CompilerServices.NullableAttribute", "Type : System.Runtime.CompilerServices.NullableContextAttribute", "Type : HelpAttribute","Type : InterrogateHelpUrls"], additionalFiles:["HelpAttribute.cs"], executionArgs:["RuntimeAttributeInstanceRetrieval"]} -->
> ```csharp
> public sealed class InterrogateHelpUrls
> {
Expand Down
8 changes: 4 additions & 4 deletions standard/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ All members of a generic class can use type parameters from any enclosing class,
> class C<V>
> {
> public V f1;
> public C<V> f2 = null;
> public C<V> f2 = null!;
>
> public C(V x)
> {
Expand Down Expand Up @@ -2390,13 +2390,13 @@ When performing overload resolution, a method with a parameter array might be ap
> ```csharp
> class Test
> {
> static void F(params string[] array) =>
> static void F(params string?[]? array) =>
> Console.WriteLine(array == null);
>
> static void Main()
> {
> F(null);
> F((string) null);
> F((string) null!);
> }
> }
> ```
Expand Down Expand Up @@ -5136,7 +5136,7 @@ Finalizers are invoked automatically, and cannot be invoked explicitly. An insta
> {
> static void Main()
> {
> B b = new B();
> B? b = new B();
> b = null;
> GC.Collect();
> GC.WaitForPendingFinalizers();
Expand Down
12 changes: 6 additions & 6 deletions standard/delegates.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,22 +291,22 @@ Attempting to invoke a delegate instance whose value is `null` results in an exc
> cd1(-1); // call M1
> D cd2 = new D(C.M2);
> cd2(-2); // call M2
> D cd3 = cd1 + cd2;
> D? cd3 = cd1 + cd2;
> cd3(10); // call M1 then M2
> cd3 += cd1;
> cd3(20); // call M1, M2, then M1
> C c = new C();
> D cd4 = new D(c.M3);
> cd3 += cd4;
> cd3(30); // call M1, M2, M1, then M3
> cd3!(30); // call M1, M2, M1, then M3
> cd3 -= cd1; // remove last M1
> cd3(40); // call M1, M2, then M3
> cd3!(40); // call M1, M2, then M3
> cd3 -= cd4;
> cd3(50); // call M1 then M2
> cd3!(50); // call M1 then M2
> cd3 -= cd2;
> cd3(60); // call M1
> cd3!(60); // call M1
> cd3 -= cd2; // impossible removal is benign
> cd3(60); // call M1
> cd3!(60); // call M1
> cd3 -= cd1; // invocation list is empty so cd3 is null
> // cd3(70); // System.NullReferenceException thrown
> cd3 -= cd1; // impossible removal is benign
Expand Down
8 changes: 4 additions & 4 deletions standard/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3218,7 +3218,7 @@ Except for the `stackalloc` operator, C# provides no predefined constructs for m
>
> public class Widget<T>
> {
> public static implicit operator Widget<T>(Span<double> sp) { return null; }
> public static implicit operator Widget<T>(Span<double> sp) { return null!; }
> }
> ```
>
Expand Down Expand Up @@ -3800,7 +3800,7 @@ The predefined addition operators are listed below. For numeric and enumeration
> {
> static void Main()
> {
> string s = null;
> string? s = null;
> Console.WriteLine("s = >" + s + "<"); // Displays s = ><
>
> int i = 1;
Expand Down Expand Up @@ -6389,8 +6389,8 @@ The run-time processing of a simple assignment of the form `x = y` with type `T`
>
> <!-- Example: {template:"standalone-console", name:"SimpleAssignment1", expectedException:"ArrayTypeMismatchException"} -->
> ```csharp
> string[] sa = new string[10];
> object[] oa = sa;
> string?[] sa = new string[10];
> object?[] oa = sa;
> oa[0] = null; // OK
> oa[1] = "Hello"; // OK
> oa[2] = new ArrayList(); // ArrayTypeMismatchException
Expand Down
2 changes: 1 addition & 1 deletion standard/lexical-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ The following pre-processing directives are available:
- `#region` and `#endregion`, which are used to explicitly mark sections of source code ([§6.5.7](lexical-structure.md#657-region-directives)).
- `#pragma`, which is used to specify optional contextual information to a compiler ([§6.5.9](lexical-structure.md#659-pragma-directives)).
- `#nullable`, which is used to specify the nullable context (§Nullable-Directives).
-

A pre-processing directive always occupies a separate line of source code and always begins with a `#` character and a pre-processing directive name. White space may occur before the `#` character and between the `#` character and the directive name.

A source line containing a `#define`, `#undef`, `#if`, `#elif`, `#else`, `#endif`, `#line`, `#endregion`, or `#nullable` directive can end with a single-line comment. Delimited comments (the `/* */` style of comments) are not permitted on source lines containing pre-processing directives.
Expand Down
2 changes: 1 addition & 1 deletion standard/statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -1926,7 +1926,7 @@ using (ResourceType rN = eN)
> }
> using (TextReader r = File.OpenText("log.txt"))
> {
> string s;
> string? s;
> while ((s = r.ReadLine()) != null)
> {
> Console.WriteLine(s);
Expand Down
4 changes: 2 additions & 2 deletions standard/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ When the nullable annotation context is disabled
- No warning shall be generated when a variable of a reference type that possibly has the null value, is dereferenced.
- For any reference type `T`, the annotation `?` in `T?` is ignored, as `T` is already a nullable type. An informational message shall be generated to that effect.
> *Note*: This message is characterized as “informational” rather than “warning,” so as not to confuse it with the state of the nullable warning context, which is unrelated. *end note*
- The null-forgiving operator `!` (§Null-Forgiving-Expressions) is ignored.
- The null-forgiving operator `!` is ignored.

> *Example*:
>
Expand All @@ -765,7 +765,7 @@ When the nullable annotation context is enabled
- For any reference type `T`, the annotation `?` in `T?` makes `T?` a nullable type, whereas the unannotated `T` is non-nullable.
- A warning shall be generated when a variable of an unannotated reference type is initialized with, or assigned a value of, `null`.
- A warning shall be generated when a variable of a nullable or non-nullable reference type, whose value might be `null`, is dereferenced; otherwise, that variable may be dereferenced safely.
- The null-forgiving operator `!` Null-Forgiving-Expressions) suppresses warnings about dereferencing a possible null reference.
- The null-forgiving operator `!` suppresses warnings about dereferencing a possible null reference.
> *Example*:
>
Expand Down
5 changes: 3 additions & 2 deletions tools/example-templates/additional-files/HelpAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ public class HelpAttribute : Attribute
{
public HelpAttribute(string url) // url is a positional parameter
{
Url = url;
}

// Topic is a named parameter
public string Topic
{
get;
set;
}
} = null!;

public string Url { get; }
public string Url { get; } = null!;
}
2 changes: 1 addition & 1 deletion tools/example-templates/standalone-console/Project.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
<AssemblyName>$example-name</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
Expand Down

0 comments on commit dcb3731

Please sign in to comment.