Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small cleanup in hot path of NameResolution #1211

Merged
merged 5 commits into from
May 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/fsharp/ConstraintSolver.fs
Original file line number Diff line number Diff line change
Expand Up @@ -654,15 +654,15 @@ let CheckWarnIfRigid (csenv:ConstraintSolverEnv) ty1 (r:Typar) ty =
/// Propagate all effects of adding this constraint, e.g. to solve other variables
let rec SolveTyparEqualsTyp (csenv:ConstraintSolverEnv) ndeep m2 trace ty1 ty =
let m = csenv.m
let denv = csenv.DisplayEnv

DepthCheck ndeep m ++ (fun () ->
match ty1 with
| TType_var r | TType_measure (MeasureVar r) ->
// The types may still be equivalent due to abbreviations, which we are trying not to eliminate
if typeEquiv csenv.g ty1 ty then CompleteD else

// The famous 'occursCheck' check to catch "infinite types" like 'a = list<'a> - see also https://github.com/Microsoft/visualfsharp/issues/1170
if occursCheck csenv.g r ty then ErrorD (ConstraintSolverInfiniteTypes(csenv.eContextInfo,denv,ty1,ty,m,m2)) else
if occursCheck csenv.g r ty then ErrorD (ConstraintSolverInfiniteTypes(csenv.eContextInfo,csenv.DisplayEnv,ty1,ty,m,m2)) else

// Note: warn _and_ continue!
CheckWarnIfRigid csenv ty1 r ty ++ (fun () ->
Expand Down Expand Up @@ -767,12 +767,10 @@ and SolveTypEqualsTyp (csenv:ConstraintSolverEnv) ndeep m2 (trace: OptionalTrace
| _ -> localAbortD

and SolveTypEqualsTypKeepAbbrevs csenv ndeep m2 trace ty1 ty2 =
let denv = csenv.DisplayEnv

// Back out of expansions of type abbreviations to give improved error messages.
// Note: any "normalization" of equations on type variables must respect the trace parameter
TryD (fun () -> SolveTypEqualsTyp csenv ndeep m2 trace ty1 ty2)
(function LocallyAbortOperationThatLosesAbbrevs -> ErrorD(ConstraintSolverTypesNotInEqualityRelation(denv,ty1,ty2,csenv.m,m2))
(function LocallyAbortOperationThatLosesAbbrevs -> ErrorD(ConstraintSolverTypesNotInEqualityRelation(csenv.DisplayEnv,ty1,ty2,csenv.m,m2))
| err -> ErrorD err)

and SolveTypEqualsTypEqns csenv ndeep m2 trace origl1 origl2 =
Expand Down
48 changes: 28 additions & 20 deletions src/fsharp/NameResolution.fs
Original file line number Diff line number Diff line change
Expand Up @@ -694,25 +694,33 @@ let rec AddModuleOrNamespaceRefsToNameEnv g amap m root ad nenv (modrefs: Module

/// Add the contents of a module or namespace to the name resolution environment
and AddModuleOrNamespaceContentsToNameEnv (g:TcGlobals) amap (ad:AccessorDomain) m root nenv (modref:ModuleOrNamespaceRef) =
let pri = NextExtensionMethodPriority()
let mty = modref.ModuleOrNamespaceType
let tycons = mty.TypeAndExceptionDefinitions

let exncs = mty.ExceptionDefinitions
let nenv = { nenv with eDisplayEnv= nenv.eDisplayEnv.AddOpenModuleOrNamespace modref }
let tcrefs = tycons |> List.map modref.NestedTyconRef |> List.filter (IsEntityAccessible amap m ad)
let exrefs = exncs |> List.map modref.NestedTyconRef |> List.filter (IsEntityAccessible amap m ad)
let nenv = (nenv,exrefs) ||> List.fold (AddExceptionDeclsToNameEnv BulkAdd.Yes)
let nenv = (nenv,tcrefs) ||> AddTyconRefsToNameEnv BulkAdd.Yes false g amap m false
let vrefs =
mty.AllValsAndMembers.ToFlatList()
|> FlatList.choose (fun x ->
if IsAccessible ad x.Accessibility then TryMkValRefInModRef modref x
else None)
|> FlatList.toArray
let nenv = AddValRefsToNameEnvWithPriority BulkAdd.Yes pri nenv vrefs
let nenv = (nenv,MakeNestedModuleRefs modref) ||> AddModuleOrNamespaceRefsToNameEnv g amap m root ad
nenv
let pri = NextExtensionMethodPriority()
let mty = modref.ModuleOrNamespaceType

let nenv =
let mutable state = { nenv with eDisplayEnv = nenv.eDisplayEnv.AddOpenModuleOrNamespace modref }

for exnc in mty.ExceptionDefinitions do
let tcref = modref.NestedTyconRef exnc
if IsEntityAccessible amap m ad tcref then
state <- AddExceptionDeclsToNameEnv BulkAdd.Yes state tcref

state

let tcrefs =
mty.TypeAndExceptionDefinitions
|> List.choose (fun tycon ->
let tcref = modref.NestedTyconRef tycon
if IsEntityAccessible amap m ad tcref then Some(tcref) else None)

let nenv = (nenv,tcrefs) ||> AddTyconRefsToNameEnv BulkAdd.Yes false g amap m false
let vrefs =
mty.AllValsAndMembers.ToFlatList()
|> FlatList.choose (fun x -> if IsAccessible ad x.Accessibility then TryMkValRefInModRef modref x else None)
|> FlatList.toArray
let nenv = AddValRefsToNameEnvWithPriority BulkAdd.Yes pri nenv vrefs
let nenv = (nenv,MakeNestedModuleRefs modref) ||> AddModuleOrNamespaceRefsToNameEnv g amap m root ad
nenv

/// Add a set of modules or namespaces to the name resolution environment
//
Expand All @@ -722,7 +730,7 @@ and AddModuleOrNamespaceContentsToNameEnv (g:TcGlobals) amap (ad:AccessorDomain)
// open M1
//
// The list contains [M1b; M1a]
and AddModulesAndNamespacesContentsToNameEnv g amap ad m root nenv modrefs =
and AddModulesAndNamespacesContentsToNameEnv g amap ad m root nenv modrefs =
(modrefs, nenv) ||> List.foldBack (fun modref acc -> AddModuleOrNamespaceContentsToNameEnv g amap ad m root acc modref)

/// Add a single modules or namespace to the name resolution environment
Expand Down
5 changes: 1 addition & 4 deletions src/fsharp/TypeChecker.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5141,16 +5141,13 @@ and TcExprFlex cenv flex ty (env: TcEnv) tpenv (e: SynExpr) =


and TcExpr cenv ty (env: TcEnv) tpenv (expr: SynExpr) =

let m = expr.Range

// Start an error recovery handler
// Note the try/catch can lead to tail-recursion problems for iterated constructs, e.g. let... in...
// So be careful!
try
TcExprNoRecover cenv ty env tpenv expr
with e ->

let m = expr.Range
// Error recovery - return some rubbish expression, but replace/annotate
// the type of the current expression with a type variable that indicates an error
errorRecovery e m
Expand Down