Steps to Reproduce:
Use #error version:7.1 with LangVersion set to 7.1
Expected Behavior:
No diagnostic
Actual Behavior:
error CS8302: Feature 'version' is not available in C# 7.1. Please use language version 7.1 or greater.
The error message looks very strange.
The problem lies in DirectiveParser.cs, more specifically, this part of code:
if (this.Options.LanguageVersion != LanguageVersion.Preview &&
errorText.StartsWith(versionMarker, StringComparison.Ordinal) &&
LanguageVersionFacts.TryParse(errorText.Substring(versionMarker.Length), out var languageVersion))
{
ErrorCode error = this.Options.LanguageVersion.GetErrorCode();
eod = this.AddError(eod, triviaOffset, triviaWidth, error, "version", new CSharpRequiredLanguageVersion(languageVersion));
}
I think the if condition needs to compare the parsed languageVersion with this.Options.LanguageVersion or this.Options.SpecifiedLanguageVersion (not sure what's the difference between them, hope someone can clear this up for me).
It's not clear to me what the purpose of #error version:7.1 is. It seems to cause us to give a LangVersion diagnostic, using the indicated language version as the required language version. Do you know what this is for @jcouv?
It's a little bit funny in its behavior, for example in a C# 6 project with #error version:5 I get: Feature 'version' is not available in C# 6. Please use language version 5 or greater.
As far as I know #error version is not a documented or supported feature, it is just a mechanism we use from time to time to try and determine what compiler is being used when compiling in some indirect way.
It's not clear to me what the purpose of #error version:7.1 is. It seems to cause us to give a LangVersion diagnostic, using the indicated language version as the required language version. Do you know what this is for @jcouv?
@RikkiGibson It's unclear to me what's the purpose of it. But the error message looks weird.
As far as I know #error version is not a documented or supported feature, it is just a mechanism we use from time to time to try and determine what compiler is being used when compiling in some indirect way.
Whoops, I recently added a PR in dotnet/docs adding information about #error version. Should I revert it?
Whoops, I recently added a PR in dotnet/docs adding information about #error version. Should I revert it?
I think it's fine.
Should I revert it?
I think it's okay to document #error version as it has been very useful for troubleshooting, but I would not document #error version:<version> as this is mostly useful to the compiler team.
Since #error version:<version> is not meant for users, I don't worry about the quality of the diagnostic message. I'll go ahead and close this issue as by-design. Thanks