未验证 提交 ae068b7d 编写于 作者: C Chris Sienkiewicz 提交者: GitHub

Make Generated syntax trees restore to project-level nullability (#35018)

* Make Generated syntax trees restore to project nullability
上级 73a0bda9
......@@ -361,9 +361,7 @@ A syntax tree is determined to be generated if meets one or more of the followin
- `<autogenerated`
- `<auto-generateed`
Newer, nullable-aware generators, may then opt-in to nullable analysis by including a generated `#nullable enable` at the beginning of the generated code.
Within the context of a generated syntax tree `#nullable restore` will always set the state to "disable".
Newer, nullable-aware generators may then opt-in to nullable analysis by including a generated `#nullable restore` at the beginning of the code, ensuring that the generated syntax tree will be analyzed according to the project-level nullable context.
## Public APIs
There are a few questions that an API consumer would want to answer:
......
......@@ -55,7 +55,8 @@ private NullableDirectiveMap(ImmutableArray<(int Position, bool? State)> directi
index = ~index - 1;
}
bool? state = null;
// Generated files have an initial nullable context that is "disabled"
bool? state = (_isGeneratedCode) ? false : (bool?)null;
if (index >= 0)
{
Debug.Assert(_directives[index].Position <= position);
......@@ -63,12 +64,6 @@ private NullableDirectiveMap(ImmutableArray<(int Position, bool? State)> directi
state = _directives[index].State;
}
if (state == null && _isGeneratedCode)
{
// Generated files have a default nullable context that is "disabled".
state = false;
}
return state;
}
......
......@@ -5232,22 +5232,25 @@ static void F(object x)
}
[Fact]
public void Directive_GloballyEnabled_GeneratedCode_RestoreToDisabled()
public void Directive_GloballyEnabled_GeneratedCode_RestoreToProjectDefault()
{
var source =
@"
// <autogenerated />
#nullable enable
partial class Program
{
#nullable restore
static void F(object x)
{
x = null; // no warning, restored to disabled
x = null; // warning, restored to project default
}
}";
var comp = CreateCompilation(source, options: TestOptions.DebugDll.WithNullableContextOptions(NullableContextOptions.Enable));
comp.VerifyDiagnostics();
comp.VerifyDiagnostics(
// (8,13): warning CS8600: Converting null literal or possible null value to non-nullable type.
// x = null; // warning
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(8, 13)
);
}
[Fact]
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册