Skip to content

Commit 2cdf31c

Browse files
authored
AddBoundValue - Manually add CCUs to incremental environment (#9253)
* Manually add CCUs to incremental environment after importing of a reflection type * Minor format change * Add check for importing * Fixing build * Trying to fix it
1 parent a816851 commit 2cdf31c

1 file changed

Lines changed: 47 additions & 35 deletions

File tree

src/fsharp/fsi/fsi.fs

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,51 @@ type internal FsiDynamicCompiler
12851285

12861286
{ istate with boundValues = boundValues }, Completed itValue
12871287

1288+
let addCcusToIncrementalEnv istate ccuinfos =
1289+
let optEnv = List.fold (AddExternalCcuToOptimizationEnv tcGlobals) istate.optEnv ccuinfos
1290+
istate.ilxGenerator.AddExternalCcus (ccuinfos |> List.map (fun ccuinfo -> ccuinfo.FSharpViewOfMetadata))
1291+
{ istate with optEnv = optEnv }
1292+
1293+
let importReflectionType istate reflectionTy =
1294+
let tcImports = istate.tcImports
1295+
let tcGlobals = istate.tcGlobals
1296+
let amap = tcImports.GetImportMap()
1297+
1298+
let prevCcuinfos = tcImports.GetImportedAssemblies()
1299+
1300+
let rec import ccuinfos (ilTy: ILType) =
1301+
let ccuinfos, tinst =
1302+
(ilTy.GenericArgs, (ccuinfos, []))
1303+
||> List.foldBack (fun ilGenericArgTy (ccuInfos, tinst) ->
1304+
let ccuinfos2, ty = import ccuInfos ilGenericArgTy
1305+
(ccuinfos2 @ ccuinfos, ty :: tinst))
1306+
1307+
let ty = Import.ImportILType amap range0 tinst ilTy
1308+
let ccuinfos =
1309+
match tryTcrefOfAppTy tcGlobals ty with
1310+
| ValueSome tcref ->
1311+
match tcref.CompilationPath.ILScopeRef with
1312+
| ILScopeRef.Assembly aref ->
1313+
(tcImports.GetImportedAssemblies() |> List.find (fun x -> x.FSharpViewOfMetadata.AssemblyName = aref.Name)) :: ccuinfos
1314+
| _ ->
1315+
ccuinfos
1316+
| _ ->
1317+
ccuinfos
1318+
ccuinfos, ty
1319+
1320+
let ilTy = convertReflectionTypeToILType reflectionTy
1321+
1322+
if not (Import.CanImportILType amap range0 ilTy) then
1323+
invalidOp (sprintf "Unable to import type, %A." reflectionTy)
1324+
1325+
let ccuinfos, ty = import [] ilTy
1326+
let ccuinfos =
1327+
ccuinfos
1328+
|> List.distinctBy (fun x -> x.FSharpViewOfMetadata.AssemblyName)
1329+
|> List.filter (fun asm1 -> not (prevCcuinfos |> List.exists (fun asm2 -> asm2.FSharpViewOfMetadata.AssemblyName = asm1.FSharpViewOfMetadata.AssemblyName)))
1330+
// After we have successfully imported the type, then we can add newly resolved ccus to the env.
1331+
addCcusToIncrementalEnv istate ccuinfos, ty
1332+
12881333
member __.DynamicAssemblyName = assemblyName
12891334

12901335
member __.DynamicAssembly = (assemblyBuilder :> Assembly)
@@ -1381,10 +1426,8 @@ type internal FsiDynamicCompiler
13811426
with e ->
13821427
tcConfigB.RemoveReferencedAssemblyByPath(m,path)
13831428
reraise()
1384-
let optEnv = List.fold (AddExternalCcuToOptimizationEnv tcGlobals) istate.optEnv ccuinfos
1385-
istate.ilxGenerator.AddExternalCcus (ccuinfos |> List.map (fun ccuinfo -> ccuinfo.FSharpViewOfMetadata))
13861429
resolutions,
1387-
{ istate with tcState = tcState.NextStateAfterIncrementalFragment(tcEnv); optEnv = optEnv }
1430+
{ addCcusToIncrementalEnv istate ccuinfos with tcState = tcState.NextStateAfterIncrementalFragment(tcEnv) }
13881431

13891432

13901433
member __.EvalDependencyManagerTextFragment (packageManager:IDependencyManagerProvider,m,path: string) =
@@ -1522,37 +1565,6 @@ type internal FsiDynamicCompiler
15221565
| _ ->
15231566
None
15241567

1525-
member private this.ImportReflectionType (ctok, istate, reflectionTy) =
1526-
let amap = istate.tcImports.GetImportMap()
1527-
1528-
let resolveAssemblyRefOfILType istate (ilTy: ILType) =
1529-
let tcImports = istate.tcImports
1530-
1531-
if ilTy.IsNominal then
1532-
match ilTy.TypeRef.Scope with
1533-
| ILScopeRef.Assembly aref ->
1534-
// Simple name unification. If it fails, then try to resolve the assembly.
1535-
if tcImports.TryFindDllInfo(ctok, range0, aref.Name, lookupOnly = true).IsNone then
1536-
this.EvalRequireReference(ctok, istate, range0, aref.Name)
1537-
|> snd
1538-
else
1539-
istate
1540-
| _ ->
1541-
istate
1542-
else
1543-
istate
1544-
1545-
let rec import istate (ilTy: ILType) =
1546-
let istate = resolveAssemblyRefOfILType istate ilTy
1547-
let istate, tinst =
1548-
(ilTy.GenericArgs, (istate, []))
1549-
||> List.foldBack (fun ilGenericArgTy (istate, tinst) ->
1550-
let istate, ty = import istate ilGenericArgTy
1551-
(istate, ty :: tinst))
1552-
istate, Import.ImportILType amap range0 tinst ilTy
1553-
1554-
import istate (convertReflectionTypeToILType reflectionTy)
1555-
15561568
member this.AddBoundValue (ctok, errorLogger: ErrorLogger, istate, name: string, value: obj) =
15571569
try
15581570
match value with
@@ -1573,7 +1585,7 @@ type internal FsiDynamicCompiler
15731585
if PrettyNaming.IsCompilerGeneratedName name then
15741586
invalidArg "name" (FSComp.SR.lexhlpIdentifiersContainingAtSymbolReserved() |> snd)
15751587

1576-
let istate, ty = this.ImportReflectionType(ctok, istate, value.GetType())
1588+
let istate, ty = importReflectionType istate (value.GetType())
15771589
let amap = istate.tcImports.GetImportMap()
15781590

15791591
let i = nextFragmentId()

0 commit comments

Comments
 (0)