Skip to content

SourcePropertySymbol constructor sometimes sets the property twice #35381

@jcouv

Description

@jcouv

In some error scenarios (bad overrides) we set the type of the property a second time.
The problem is that the first type matters (it causes side-effects) which are not undone when the type is set the second time.

See the constructor for SourcePropertySymbol:

                    // We do an extra check before copying the type to handle the case where the overriding
                    // property (incorrectly) has a different type than the overridden property.  In such cases,
                    // we want to retain the original (incorrect) type to avoid hiding the type given in source.
                    if (type.Type.Equals(overriddenPropertyType.Type, TypeCompareKind.IgnoreCustomModifiersAndArraySizesAndLowerBounds | TypeCompareKind.IgnoreNullableModifiersForReferenceTypes | TypeCompareKind.IgnoreDynamic))
                    {
                        type = type.WithTypeAndModifiers(
                            CustomModifierUtils.CopyTypeCustomModifiers(overriddenPropertyType.Type, type.Type, this.ContainingAssembly),
                            overriddenPropertyType.CustomModifiers);

                        // Although we only do this in error scenarios, it is undesirable to mutate the symbol by setting its type twice.
                        // Tracked by https://github.com/dotnet/roslyn/issues/35381
                        Interlocked.Exchange(ref _lazyType, null);
                        Interlocked.CompareExchange(ref _lazyType, new StrongBox<TypeWithAnnotations>(type), null);
                    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions