コード例 #1
0
        internal void ComputeReturnType()
        {
            if (!_lazyReturnType.IsDefault)
            {
                return;
            }

            var        diagnostics               = DiagnosticBag.GetInstance();
            TypeSyntax returnTypeSyntax          = _syntax.ReturnType;
            TypeSymbolWithAnnotations returnType = _binder.BindType(returnTypeSyntax.SkipRef(), diagnostics);

            if (this.IsAsync)
            {
                if (this.RefKind != RefKind.None)
                {
                    ReportBadRefToken(returnTypeSyntax, diagnostics);
                }
                else if (returnType.TypeSymbol.IsBadAsyncReturn(this.DeclaringCompilation))
                {
                    diagnostics.Add(ErrorCode.ERR_BadAsyncReturn, this.Locations[0]);
                }
            }

            var location = _syntax.ReturnType.Location;

            if (_refKind == RefKind.RefReadOnly)
            {
                DeclaringCompilation.EnsureIsReadOnlyAttributeExists(diagnostics, location, modifyCompilation: false);
            }

            if (returnType.NeedsNullableAttribute())
            {
                DeclaringCompilation.EnsureNullableAttributeExists(diagnostics, location, modifyCompilation: false);
                // Note: we don't need to warn on annotations used without NonNullTypes context for local functions, as this is handled in binding already
            }

            // span-like types are returnable in general
            if (returnType.IsRestrictedType(ignoreSpanLikeTypes: true))
            {
                // Method or delegate cannot return type '{0}'
                diagnostics.Add(ErrorCode.ERR_MethodReturnCantBeRefAny, returnTypeSyntax.Location, returnType.TypeSymbol);
            }

            Debug.Assert(_refKind == RefKind.None ||
                         returnType.SpecialType != SpecialType.System_Void ||
                         returnTypeSyntax.HasErrors);

            lock (_declarationDiagnostics)
            {
                if (!_lazyReturnType.IsDefault)
                {
                    diagnostics.Free();
                    return;
                }

                _declarationDiagnostics.AddRangeAndFree(diagnostics);
                _lazyReturnType = returnType;
            }
        }
コード例 #2
0
            internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions, DiagnosticBag diagnostics)
            {
                base.AfterAddingTypeMembersChecks(conversions, diagnostics);

                if (_refKind == RefKind.RefReadOnly)
                {
                    var syntax = (DelegateDeclarationSyntax)SyntaxRef.GetSyntax();
                    DeclaringCompilation.EnsureIsReadOnlyAttributeExists(diagnostics, syntax.ReturnType.GetLocation(), modifyCompilationForRefReadOnly: true);
                }

                ParameterHelpers.EnsureIsReadOnlyAttributeExists(Parameters, diagnostics, modifyCompilationForRefReadOnly: true);
            }
コード例 #3
0
        internal void ComputeReturnType()
        {
            if (!_lazyReturnType.IsNull)
            {
                return;
            }

            var        diagnostics               = DiagnosticBag.GetInstance();
            TypeSyntax returnTypeSyntax          = _syntax.ReturnType;
            TypeSymbolWithAnnotations returnType = _binder.BindType(returnTypeSyntax.SkipRef(), diagnostics);

            if (this.IsAsync)
            {
                if (this.RefKind != RefKind.None)
                {
                    ReportBadRefToken(returnTypeSyntax, diagnostics);
                }
                else if (returnType.TypeSymbol.IsBadAsyncReturn(this.DeclaringCompilation))
                {
                    diagnostics.Add(ErrorCode.ERR_BadAsyncReturn, this.Locations[0]);
                }
            }

            var location = _syntax.ReturnType.Location;

            if (_refKind == RefKind.RefReadOnly)
            {
                DeclaringCompilation.EnsureIsReadOnlyAttributeExists(diagnostics, location, modifyCompilation: false);
            }

            if (returnType.ContainsNullableReferenceTypes())
            {
                DeclaringCompilation.EnsureNullableAttributeExists(diagnostics, location, modifyCompilation: false);
                // Note: we don't need to warn on annotations used without NonNullTypes context for local functions, as this is handled in binding already
            }

            Debug.Assert(_refKind == RefKind.None ||
                         returnType.SpecialType != SpecialType.System_Void ||
                         returnTypeSyntax.HasErrors);

            lock (_declarationDiagnostics)
            {
                if (!_lazyReturnType.IsNull)
                {
                    diagnostics.Free();
                    return;
                }

                _declarationDiagnostics.AddRangeAndFree(diagnostics);
                _lazyReturnType = returnType;
            }
        }
コード例 #4
0
        internal void ComputeReturnType()
        {
            if (_lazyReturnType != null)
            {
                return;
            }

            var        diagnostics      = DiagnosticBag.GetInstance();
            TypeSyntax returnTypeSyntax = _syntax.ReturnType.SkipRef();
            TypeSymbol returnType       = _binder.BindType(returnTypeSyntax, diagnostics);

            if (IsAsync &&
                returnType.SpecialType != SpecialType.System_Void &&
                !returnType.IsNonGenericTaskType(_binder.Compilation) &&
                !returnType.IsGenericTaskType(_binder.Compilation))
            {
                // The return type of an async method must be void, Task or Task<T>
                diagnostics.Add(ErrorCode.ERR_BadAsyncReturn, this.Locations[0]);
            }

            if (_refKind == RefKind.RefReadOnly)
            {
                DeclaringCompilation.EnsureIsReadOnlyAttributeExists(diagnostics, _syntax.ReturnType.Location, modifyCompilationForRefReadOnly: false);
            }

            Debug.Assert(_refKind == RefKind.None ||
                         returnType.SpecialType != SpecialType.System_Void ||
                         returnTypeSyntax.HasErrors);

            lock (_declarationDiagnostics)
            {
                if (_lazyReturnType != null)
                {
                    diagnostics.Free();
                    return;
                }

                _declarationDiagnostics.AddRangeAndFree(diagnostics);
                _lazyReturnType = returnType;
            }
        }
コード例 #5
0
            internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions, DiagnosticBag diagnostics)
            {
                var syntax   = (DelegateDeclarationSyntax)SyntaxRef.GetSyntax();
                var location = syntax.ReturnType.GetLocation();

                Debug.Assert(location != null);

                base.AfterAddingTypeMembersChecks(conversions, diagnostics);

                if (_refKind == RefKind.RefReadOnly)
                {
                    DeclaringCompilation.EnsureIsReadOnlyAttributeExists(diagnostics, location, modifyCompilation: true);
                }

                ParameterHelpers.EnsureIsReadOnlyAttributeExists(Parameters, diagnostics, modifyCompilation: true);

                if (ReturnTypeWithAnnotations.NeedsNullableAttribute())
                {
                    this.DeclaringCompilation.EnsureNullableAttributeExists(diagnostics, location, modifyCompilation: true);
                }

                ParameterHelpers.EnsureNullableAttributeExists(Parameters, diagnostics, modifyCompilation: true);
            }