Skip to content

6.0.400 compilation error with private types in namespace used in a different file #13661

Closed

Description

@l3m

Repro steps

Defining a private type in a namespace and using it in a module in another file in the same namespace leads to a compilation error in 6.0.400.

Compiles without error in 6.0.300.

A simple repro can be found here: https://github.com/l3m/fsharp-error-repro

Expected behavior

Unclear if the behavior in 6.0.300 or 6.0.400 is the actually correct one, but it's certainly a change.

So the expectation would have been that it compiles in 6.0.400

Actual behavior

Fails to compile in 6.0.400

Known workarounds

Replace

type private Blah = ... 

with

type internal Blah = ... 

Related information

Observed on both linux and windows, with dotnet sdk 6.0.300 and 6.0.400

Activity

  1. changed the title [-]Compilation error with private types in namespace when upgrading to 6.0.400[/-] [+]6.0.400 compilation error with private types in namespace used in a different file[/+] on Aug 10, 2022
  2. vzarytovskii commented on Aug 10, 2022

    @vzarytovskii
    Member

    Looks like this suggestion been fixed
    fsharp/fslang-suggestions#389
    Let me bisect and find where it was changed.

  3. vzarytovskii commented on Aug 10, 2022

    @vzarytovskii
    Member

    Ok, with help of @0101, we bisected to that PR: #13287
    CC @nojaf @dsyme

  4. nojaf commented on Aug 11, 2022

    @nojaf
    Contributor

    @vzarytovskii I can't really put my finger on it.
    For some reason the Blah type is not being added to

    eTyconsByAccessNames: LayeredMultiMap<string, TyconRef>

    and it is later not found in

    match LookupTypeNameInEnvHaveArity fullyQualified id.idText staticResInfo.NumStaticArgs nenv with

  5. nojaf commented on Aug 11, 2022

    @nojaf
    Contributor

    The problem appears to go away with a CustomEquality for type ModuleOrNamespaceKind.

    [<CustomEquality; NoComparison>]
    type ModuleOrNamespaceKind =
    
        /// Indicates that a module is compiled to a class with the "Module" suffix added. 
        | FSharpModuleWithSuffix 
    
        /// Indicates that a module is compiled to a class with the same name as the original module 
        | ModuleOrType 
    
        /// Indicates that a 'module' is really a namespace 
        | Namespace of
            /// Indicates that the sourcecode had a namespace.
            /// If false, this namespace was implicitly constructed during type checking. 
            isExplicit: bool
            
        override this.Equals other =
            match other with
            | :? ModuleOrNamespaceKind as kind ->
                match this, kind with
                | FSharpModuleWithSuffix, FSharpModuleWithSuffix
                | ModuleOrType, ModuleOrType
                | Namespace _, Namespace _ -> true
                | _ -> false
            | _ -> false
    
        override this.GetHashCode () =
            match this with
            | FSharpModuleWithSuffix -> 0
            | ModuleOrType -> 1
            | Namespace _ -> 2

    @vzarytovskii

  6. vzarytovskii commented on Aug 11, 2022

    @vzarytovskii
    Member

    Huh, interesting. Do did lookup work before?

  7. nojaf commented on Aug 11, 2022

    @nojaf
    Contributor

    I could be way off here but I think some structure that contains a ModuleOrNamespaceKind is being compared somewhere.

    For example:

    type CompilationPath =
    | CompPath of ILScopeRef * (string * ModuleOrNamespaceKind) list
    member x.ILScopeRef = let (CompPath(scoref, _)) = x in scoref
    member x.AccessPath = let (CompPath(_, p)) = x in p
    member x.MangledPath = List.map fst x.AccessPath
    member x.NestedPublicPath (id: Ident) = PubPath(Array.append (Array.ofList x.MangledPath) [| id.idText |])
    member x.ParentCompPath =
    let a, _ = List.frontAndBack x.AccessPath
    CompPath(x.ILScopeRef, a)
    member x.NestedCompPath n moduleKind =
    CompPath(x.ILScopeRef, x.AccessPath@[(n, moduleKind)])
    member x.DemangledPath =
    x.AccessPath |> List.map (fun (nm, k) -> CompilationPath.DemangleEntityName nm k)
    /// String 'Module' off an F# module name, if FSharpModuleWithSuffix is used
    static member DemangleEntityName nm k =
    match k with
    | FSharpModuleWithSuffix -> String.dropSuffix nm FSharpModuleSuffix
    | _ -> nm

  8. dsyme commented on Aug 11, 2022

    @dsyme
    Contributor

    Subtle introduction of the bug. Marking CompilationPath as NoEquality may help track track this down, looks like it is the structural equality on this:

    [<StructuralEquality; NoComparison; StructuredFormatDisplay("{DebugText}")>]
    type Accessibility = 
        | TAccess of compilationPaths: CompilationPath list
  9. dsyme commented on Aug 11, 2022

    @dsyme
    Contributor

    By using this diff https://github.com/dotnet/fsharp/compare/main...dsyme:cwe?expand=1 it looks like these are the origin points for structural equality

    
    let isPublicVal (lv: Val) = (lv.Accessibility = taccessPublic)
    let isPublicUnionCase (ucr: UnionCaseRef) = (ucr.UnionCase.Accessibility = taccessPublic)
    let isPublicRecdField (rfr: RecdFieldRef) = (rfr.RecdField.Accessibility = taccessPublic)
    let isPublicTycon (tcref: Tycon) = (tcref.Accessibility = taccessPublic)
    

    Also this in AccessibilityLogic:

            | AccessibleFrom(cs1, tc1), AccessibleFrom(cs2, tc2) -> (cs1 = cs2) && (match tc1, tc2 with None, None -> true | Some tc1, Some tc2 -> tyconRefEq g tc1 tc2 | _ -> false)

    One option is to switch to NoEquality and NoComparison on Accessibility and CompilationPath and use an explicit equality routine in the above places.

    Another is to use the custom equality like @nojaf above, though I think perhaps just making everything more explicit is better. There are enough notions of equality needed in the compiler its better to be explicit for anything in TypedTree

  10. 0101 commented on Aug 15, 2022

    @0101
    Contributor

    This particular case seems to be caused by the comparison made here:

    let inline canAccessCompPathFrom (CompPath(scoref1, cpath1)) (CompPath(scoref2, cpath2)) =
    let rec loop p1 p2 =
    match p1, p2 with
    | (a1, k1) :: rest1, (a2, k2) :: rest2 -> (a1=a2) && (k1=k2) && loop rest1 rest2
    | [], _ -> true
    | _ -> false // cpath1 is longer
    loop cpath1 cpath2 &&
    (scoref1 = scoref2)

    But I imagine it could also cause a similar issue here:

    let canAccessCompPathFromCrossProject (CompPath(scoref1, cpath1)) (CompPath(scoref2, cpath2)) =
    let rec loop p1 p2 =
    match p1, p2 with
    | (a1, k1) :: rest1, (a2, k2) :: rest2 -> (a1=a2) && (k1=k2) && loop rest1 rest2
    | [], _ -> true
    | _ -> false // cpath1 is longer
    loop cpath1 cpath2 &&
    nameOfScoRef thisCcu1 scoref1 = nameOfScoRef thisCcu2 scoref2

    Switching Accessibility and CompilationPath to NoEquality wouldn't really force the fix here since the equality comparison is performed directly on ModuleOrNamespaceKind.

    We can mark that one as NoEquality but that will add quite a bit of extra equality code in different places. So I would lean more towards @nojaf 's custom equality there. Especially since I don't see a need for the current default version where a implicitly defined namespace is considered different than the same but explicitly defined one (though I might be wrong there).

  11. linked a pull request that will close this issueCustom equality on ModuleOrNamespaceKind #13693on Aug 15, 2022
  12. added
    Area-Compiler-Checking
    on Aug 16, 2022
  13. added this to the Backlog milestone on Aug 16, 2022
  14. Repository owner moved this from Not Planned to Done in F# Compiler and Toolingon Aug 16, 2022
  15. stroborobo commented on Sep 14, 2022

    @stroborobo

    Is there a chance we could get this fix rolled out soon? The SDK version 6.0.401 (released yesterday) is still failing to build our project even though the fix was merged one month ago.

  16. vzarytovskii commented on Sep 14, 2022

    @vzarytovskii
    Member

    Is there a chance we could get this fix rolled out soon? The SDK version 6.0.401 (released yesterday) is still failing to build our project even though the fix was merged one month ago.

    Fix was only merged to .NET7 branch, we couldn't do servicing to 401, it was too late. One of future 6.x.x servicing is a target.

  17. stroborobo commented on Sep 14, 2022

    @stroborobo

    Ok, thanks for the quick response. We'll have to hold out another month or two then. :'(

  18. vzarytovskii commented on Sep 14, 2022

    @vzarytovskii
    Member

    Ok, thanks for the quick response. We'll have to hold out another month or two then. :'(

    Yeah, sorry for that, was a bit unfortunate timing on our side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area-Compiler-CheckingBugRegression

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions