antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

Emit nullable annotations for c# 8.0

Open YairHalberstadt opened this issue 4 years ago • 10 comments

Currently antlr generates nullable attributes to indicate nullability when targeting C#.

It would be extremely useful if there was an option to emit C# 8.0 nullable annotations instead.

YairHalberstadt avatar Sep 04 '19 07:09 YairHalberstadt

@sharwell

I wonder if you have any thoughts on how difficult this would be to cover completely? I imagine the first step would be to cover the nuget packages with nullable annotations - I'm happy to do that, but I don't know if I have enough time to add an option to emit nullable annotations.

YairHalberstadt avatar Sep 04 '19 08:09 YairHalberstadt

@YairHalberstadt I'm planning to update tunnelvisionlabs/antlr4cs using the nullability infrastructure we created for tunnelvisionlabs/antlr4ts.

sharwell avatar Sep 10 '19 07:09 sharwell

Does the issue https://github.com/antlr/antlr4/issues/1972 influence this one? There is no distinction between missing token due to the optional operator (?) and due to a syntax error.

KvanTTT avatar Sep 10 '19 15:09 KvanTTT

@KvanTTT That issue impacts the usability of the syntax tree when errors occur, including but not limited to languages that represent null in the type system.

sharwell avatar Sep 10 '19 19:09 sharwell

@sharwell, can I please just double-check the latest state of this issue? Am I right in saying that currently generated C# code will not distinguish between a value which might be null and which might not (according to the grammar schema)? i.e. the generated C# will always say something like public SomeContext someThing; and never public SomeContext? someThing;. That's what I'm seeing on my system, but not sure if I'm just doing something wrong. Thanks!

yonigibbs avatar Nov 13 '20 15:11 yonigibbs

Am I right in saying that currently generated C# code will not distinguish between a value which might be null and which might not (according to the grammar schema)?

Yes, that's right.

sharwell avatar Nov 13 '20 15:11 sharwell

Cool, thanks!

yonigibbs avatar Nov 13 '20 15:11 yonigibbs

If nullable annotations are implemented maybe it will make sense to use the newest C# version for other constructions or at least C# 8.0? For instance, expression bodies for single-line properties. Also, it will make sense to remove Obsolete properties since grammar will be not backward compatible.

KvanTTT avatar Nov 28 '20 13:11 KvanTTT

Yes please! This is a necessity! Unfortunately C# is not very pedantic with warnings when using unclear nullability methods in a file with nullable enabled. I wish there was a pedantic mode that would treat unclear nullability as nullable, so I'd have to explicitly check null or assert non-null myself. Since it's not like this, it's very confusing to have mixed nullability, cause I never know when I have a guarantee or not.

geekley avatar Mar 10 '21 02:03 geekley

Yes please! This is a necessity! Unfortunately C# is not very pedantic with warnings when using unclear nullability methods in a file with nullable enabled. I wish there was a pedantic mode that would treat unclear nullability as nullable, so I'd have to explicitly check null or assert non-null myself. Since it's not like this, it's very confusing to have mixed nullability, cause I never know when I have a guarantee or not.

I totally agree. The nullable reference types in C# are basically a preview feature that got released. So many things broken with them.

NickStrupat avatar Jul 03 '22 17:07 NickStrupat