Exemplo n.º 1
0
        /// <remarks>
        /// NOTE: we are specifically diverging from dev11 to improve the user experience.
        /// Since treating the "async" keyword as an identifier in older language
        /// versions can never result in a correct program, we instead accept it as the a
        /// keyword regardless of the language version and produce an error if the version
        /// is insufficient.
        /// </remarks>
        protected TNode CheckFeatureAvailability <TNode>(TNode node, MessageID feature, bool forceWarning = false)
            where TNode : CSharpSyntaxNode
        {
            LanguageVersion availableVersion = this.Options.LanguageVersion;

            if (feature == MessageID.IDS_FeatureModuleAttrLoc)
            {
                // There's a special error code for this feature, so handle it separately.
                return(availableVersion >= LanguageVersion.CSharp2
                    ? node
                    : this.AddError(node, ErrorCode.WRN_NonECMAFeature, feature.Localize()));
            }

            LanguageVersion requiredVersion = feature.RequiredVersion();

            if (availableVersion >= requiredVersion)
            {
                return(node);
            }

            if (!forceWarning)
            {
                return(this.AddError(node, availableVersion.GetErrorCode(), feature.Localize(), (int)requiredVersion));
            }

            SyntaxDiagnosticInfo rawInfo = new SyntaxDiagnosticInfo(availableVersion.GetErrorCode(), feature.Localize(), (int)requiredVersion);

            return(this.AddError(node, ErrorCode.WRN_ErrorOverride, rawInfo, rawInfo.Code));
        }
Exemplo n.º 2
0
        private static CSDiagnosticInfo GetDisabledFeatureDiagnosticInfo(
            MessageID feature,
            LanguageVersion availableVersion
            )
        {
            string?requiredFeature = feature.RequiredFeature();

            if (requiredFeature != null)
            {
                return(new CSDiagnosticInfo(
                           ErrorCode.ERR_FeatureIsExperimental,
                           feature.Localize(),
                           requiredFeature
                           ));
            }

            LanguageVersion requiredVersion = feature.RequiredVersion();

            return(requiredVersion == LanguageVersion.Preview.MapSpecifiedToEffectiveVersion()
              ? new CSDiagnosticInfo(ErrorCode.ERR_FeatureInPreview, feature.Localize())
              : new CSDiagnosticInfo(
                       availableVersion.GetErrorCode(),
                       feature.Localize(),
                       new CSharpRequiredLanguageVersion(requiredVersion)
                       ));
        }
Exemplo n.º 3
0
        /// <remarks>
        /// NOTE: we are specifically diverging from dev11 to improve the user experience.
        /// Since treating the "async" keyword as an identifier in older language
        /// versions can never result in a correct program, we instead accept it as a
        /// keyword regardless of the language version and produce an error if the version
        /// is insufficient.
        /// </remarks>
        protected TNode CheckFeatureAvailability <TNode>(TNode node, MessageID feature, bool forceWarning = false)
            where TNode : GreenNode
        {
            LanguageVersion availableVersion = this.Options.LanguageVersion;

            if (feature == MessageID.IDS_FeatureModuleAttrLoc)
            {
                // There's a special error code for this feature, so handle it separately.
                return(availableVersion >= LanguageVersion.CSharp2
                    ? node
                    : this.AddError(node, ErrorCode.WRN_NonECMAFeature, feature.Localize()));
            }

            if (IsFeatureEnabled(feature))
            {
                return(node);
            }

            var featureName     = feature.Localize();
            var requiredFeature = feature.RequiredFeature();

            if (requiredFeature != null)
            {
                if (forceWarning)
                {
                    SyntaxDiagnosticInfo rawInfo = new SyntaxDiagnosticInfo(ErrorCode.ERR_FeatureIsExperimental, featureName, requiredFeature);
                    return(this.AddError(node, ErrorCode.WRN_ErrorOverride, rawInfo, rawInfo.Code));
                }

                return(this.AddError(node, ErrorCode.ERR_FeatureIsExperimental, featureName, requiredFeature));
            }
            else
            {
                var requiredVersion = feature.RequiredVersion();

                if (forceWarning)
                {
                    SyntaxDiagnosticInfo rawInfo = new SyntaxDiagnosticInfo(availableVersion.GetErrorCode(), featureName,
                                                                            new CSharpRequiredLanguageVersion(requiredVersion));
                    return(this.AddError(node, ErrorCode.WRN_ErrorOverride, rawInfo, rawInfo.Code));
                }

                return(this.AddError(node, availableVersion.GetErrorCode(), featureName, new CSharpRequiredLanguageVersion(requiredVersion)));
            }
        }
Exemplo n.º 4
0
        internal static void CheckFeatureAvailability(this MessageID feature, LanguageVersion availableVersion, DiagnosticBag diagnostics, Location errorLocation)
        {
            LanguageVersion requiredVersion = feature.RequiredVersion();

            if (requiredVersion > availableVersion)
            {
                diagnostics.Add(availableVersion.GetErrorCode(), errorLocation, feature.Localize(), new CSharpRequiredLanguageVersion(requiredVersion));
            }
        }
Exemplo n.º 5
0
        private static DiagnosticInfo ReportNullableReferenceTypesIfNeeded(CSharpCompilation compilation, INonNullTypesContext nonNullTypesContext)
        {
            var featureID = MessageID.IDS_FeatureNullableReferenceTypes;

            if (!compilation.IsFeatureEnabled(featureID))
            {
                LanguageVersion availableVersion = compilation.LanguageVersion;
                LanguageVersion requiredVersion  = featureID.RequiredVersion();

                return(new CSDiagnosticInfo(availableVersion.GetErrorCode(), featureID.Localize(), new CSharpRequiredLanguageVersion(requiredVersion)));
            }
            else if (nonNullTypesContext.NonNullTypes != true)
            {
                return(new CSDiagnosticInfo(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation));
            }

            return(null);
        }
Exemplo n.º 6
0
        internal static CSDiagnosticInfo GetFeatureAvailabilityDiagnosticInfo(this MessageID feature, CSharpParseOptions options)
        {
            LanguageVersion requiredVersion;

            if (options.IsFeatureEnabled(feature))
            {
                return(null);
            }

            string requiredFeature = feature.RequiredFeature();

            if (requiredFeature != null)
            {
                return(new CSDiagnosticInfo(ErrorCode.ERR_FeatureIsExperimental, feature.Localize(), requiredFeature));
            }

            LanguageVersion availableVersion = options.LanguageVersion;

            requiredVersion = feature.RequiredVersion();
            return(requiredVersion == LanguageVersion.Preview.MapSpecifiedToEffectiveVersion()
                ? new CSDiagnosticInfo(ErrorCode.ERR_FeatureInPreview, feature.Localize())
                : new CSDiagnosticInfo(availableVersion.GetErrorCode(), feature.Localize(), new CSharpRequiredLanguageVersion(requiredVersion)));
        }