提交 f444e587 编写于 作者: N ncave 提交者: Kevin Ransom (msft)

Uniform TryGetValue usage (#6598)

* Uniform TryGetValue usage

* Removed extra parenthesis

* Remove parenthesis
上级 c4c1d9cf
......@@ -41,7 +41,7 @@ let B = File1.A + File1.A"""
interface IFileSystem with
// Implement the service to open files for reading and writing
member __.FileStreamReadShim(fileName) =
match files.TryGetValue(fileName) with
match files.TryGetValue fileName with
| true, text -> new MemoryStream(Encoding.UTF8.GetBytes(text)) :> Stream
| _ -> defaultFileSystem.FileStreamReadShim(fileName)
......@@ -52,7 +52,7 @@ let B = File1.A + File1.A"""
defaultFileSystem.FileStreamWriteExistingShim(fileName)
member __.ReadAllBytesShim(fileName) =
match files.TryGetValue(fileName) with
match files.TryGetValue fileName with
| true, text -> Encoding.UTF8.GetBytes(text)
| _ -> defaultFileSystem.ReadAllBytesShim(fileName)
......
......@@ -41,7 +41,7 @@ let B = File1.A + File1.A"""
interface IFileSystem with
// 読み取りおよび書き込み用にファイルをオープンする機能を実装
member __.FileStreamReadShim(fileName) =
match files.TryGetValue(fileName) with
match files.TryGetValue fileName with
| true, text -> new MemoryStream(Encoding.UTF8.GetBytes(text)) :> Stream
| _ -> defaultFileSystem.FileStreamReadShim(fileName)
......@@ -55,7 +55,7 @@ let B = File1.A + File1.A"""
defaultFileSystem.FileStreamWriteExistingShim(fileName)
member __.ReadAllBytesShim(fileName) =
match files.TryGetValue(fileName) with
match files.TryGetValue fileName with
| true, text -> Encoding.UTF8.GetBytes(text)
| _ -> defaultFileSystem.ReadAllBytesShim(fileName)
......
......@@ -985,15 +985,13 @@ type MemoizationTable<'T, 'U>(compute: 'T -> 'U, keyComparer: IEqualityComparer<
member t.Apply x =
if (match canMemoize with None -> true | Some f -> f x) then
let mutable res = Unchecked.defaultof<'U>
let ok = table.TryGetValue(x, &res)
if ok then res
else
match table.TryGetValue x with
| true, res -> res
| _ ->
lock table (fun () ->
let mutable res = Unchecked.defaultof<'U>
let ok = table.TryGetValue(x, &res)
if ok then res
else
match table.TryGetValue x with
| true, res -> res
| _ ->
let res = compute x
table.[x] <- res
res)
......@@ -1074,11 +1072,10 @@ module Tables =
let memoize f =
let t = new Dictionary<_, _>(1000, HashIdentity.Structural)
fun x ->
let mutable res = Unchecked.defaultof<_>
if t.TryGetValue(x, &res) then
res
else
res <- f x
match t.TryGetValue x with
| true, res -> res
| _ ->
let res = f x
t.[x] <- res
res
......
......@@ -903,12 +903,11 @@ let mkCacheInt32 lowMem _inbase _nm _sz =
| null -> cache := new Dictionary<int32, _>(11)
| _ -> ()
!cache
let mutable res = Unchecked.defaultof<_>
let ok = cache.TryGetValue(idx, &res)
if ok then
match cache.TryGetValue idx with
| true, res ->
incr count
res
else
| _ ->
let res = f idx
cache.[idx] <- res
res
......@@ -4048,7 +4047,7 @@ let OpenILModuleReader fileName opts =
let cacheResult2 =
// can't used a cached entry when reading PDBs, since it makes the returned object IDisposable
if keyOk && opts.pdbDirPath.IsNone then
ilModuleReaderCache2.TryGetValue(key)
ilModuleReaderCache2.TryGetValue key
else
false, Unchecked.defaultof<_>
......
......@@ -464,10 +464,9 @@ type MetadataTable<'T> =
#if DEBUG
tbl.lookups <- tbl.lookups + 1
#endif
let mutable res = Unchecked.defaultof<_>
let ok = tbl.dict.TryGetValue(x, &res)
if ok then res
else tbl.AddSharedEntry x
match tbl.dict.TryGetValue x with
| true, res -> res
| _ -> tbl.AddSharedEntry x
/// This is only used in one special place - see further below.
......@@ -769,11 +768,12 @@ let rec GetTypeRefAsTypeRefRow cenv (tref: ILTypeRef) =
SharedRow [| ResolutionScope (rs1, rs2); nelem; nselem |]
and GetTypeRefAsTypeRefIdx cenv tref =
let mutable res = 0
if cenv.trefCache.TryGetValue(tref, &res) then res else
let res = FindOrAddSharedRow cenv TableNames.TypeRef (GetTypeRefAsTypeRefRow cenv tref)
cenv.trefCache.[tref] <- res
res
match cenv.trefCache.TryGetValue tref with
| true, res -> res
| _ ->
let res = FindOrAddSharedRow cenv TableNames.TypeRef (GetTypeRefAsTypeRefRow cenv tref)
cenv.trefCache.[tref] <- res
res
and GetTypeDescAsTypeRefIdx cenv (scoref, enc, n) =
GetTypeRefAsTypeRefIdx cenv (mkILNestedTyRef (scoref, enc, n))
......
......@@ -513,7 +513,7 @@ let IsSecurityAttribute (g: TcGlobals) amap (casmap : Dictionary<Stamp, bool>) (
match attr.TyconRef.TryDeref with
| ValueSome _ ->
let tcs = tcref.Stamp
match casmap.TryGetValue(tcs) with
match casmap.TryGetValue tcs with
| true, c -> c
| _ ->
let exists = ExistsInEntireHierarchyOfType (fun t -> typeEquiv g t (mkAppTy attr.TyconRef [])) g amap m AllowMultiIntfInstantiations.Yes (mkAppTy tcref [])
......
......@@ -336,16 +336,18 @@ module internal ExtensionTyping =
match ctxt with
| NoEntries -> None
| Entries(d, _) ->
let mutable res = Unchecked.defaultof<_>
if d.TryGetValue(st, &res) then Some res else None
match d.TryGetValue st with
| true, res -> Some res
| _ -> None
member ctxt.TryGetTyconRef st =
match ctxt with
| NoEntries -> None
| Entries(_, d) ->
let d = d.Force()
let mutable res = Unchecked.defaultof<_>
if d.TryGetValue(st, &res) then Some res else None
match d.TryGetValue st with
| true, res -> Some res
| _ -> None
member ctxt.RemapTyconRefs (f: obj->obj) =
match ctxt with
......
......@@ -107,7 +107,7 @@ type PropertyCollector(g, amap, m, ty, optFilter, ad) =
let props = new Dictionary<PropInfo, PropInfo>(hashIdentity)
let add pinfo =
match props.TryGetValue(pinfo), pinfo with
match props.TryGetValue pinfo, pinfo with
| (true, FSProp (_, ty, Some vref1, _)), FSProp (_, _, _, Some vref2)
| (true, FSProp (_, ty, _, Some vref2)), FSProp (_, _, Some vref1, _) ->
let pinfo = FSProp (g, ty, Some vref1, Some vref2)
......
......@@ -725,7 +725,7 @@ let TryImportProvidedMethodBaseAsLibraryIntrinsic (amap: Import.ImportMap, m: ra
match amap.g.knownIntrinsics.TryGetValue ((declaringEntity.LogicalName, methodName)) with
| true, vref -> Some vref
| _ ->
match amap.g.knownFSharpCoreModules.TryGetValue(declaringEntity.LogicalName) with
match amap.g.knownFSharpCoreModules.TryGetValue declaringEntity.LogicalName with
| true, modRef ->
modRef.ModuleOrNamespaceType.AllValsByLogicalName
|> Seq.tryPick (fun (KeyValue(_, v)) -> if v.CompiledName = methodName then Some (mkNestedValRef modRef v) else None)
......@@ -1219,7 +1219,7 @@ module ProvidedMethodCalls =
// sub in the appropriate argument
// REVIEW: "thisArg" pointer should be first, if present
let vRaw = pe.PUntaint(id, m)
match varConv.TryGetValue(vRaw) with
match varConv.TryGetValue vRaw with
| true, v -> v
| _ ->
let typeProviderDesignation = ExtensionTyping.DisplayNameOfTypeProvider (pe.TypeProvider, m)
......
......@@ -573,18 +573,18 @@ let GetInfoForLocalValue cenv env (v: Val) m =
// Abstract slots do not have values
if v.IsDispatchSlot then UnknownValInfo
else
let mutable res = Unchecked.defaultof<_>
let ok = cenv.localInternalVals.TryGetValue(v.Stamp, &res)
if ok then res else
match env.localExternalVals.TryFind v.Stamp with
| Some vval -> vval
| None ->
if v.MustInline then
errorR(Error(FSComp.SR.optValueMarkedInlineButWasNotBoundInTheOptEnv(fullDisplayTextOfValRef (mkLocalValRef v)), m))
match cenv.localInternalVals.TryGetValue v.Stamp with
| true, res -> res
| _ ->
match env.localExternalVals.TryFind v.Stamp with
| Some vval -> vval
| None ->
if v.MustInline then
errorR(Error(FSComp.SR.optValueMarkedInlineButWasNotBoundInTheOptEnv(fullDisplayTextOfValRef (mkLocalValRef v)), m))
#if CHECKED
warning(Error(FSComp.SR.optLocalValueNotFoundDuringOptimization(v.DisplayName), m))
warning(Error(FSComp.SR.optLocalValueNotFoundDuringOptimization(v.DisplayName), m))
#endif
UnknownValInfo
UnknownValInfo
let TryGetInfoForCcu env (ccu: CcuThunk) = env.globalModuleInfos.TryFind(ccu.AssemblyName)
......
......@@ -211,7 +211,7 @@ let IsValLocal env (v: Val) =
/// Get the limit of the val.
let GetLimitVal cenv env m (v: Val) =
let limit =
match cenv.limitVals.TryGetValue(v.Stamp) with
match cenv.limitVals.TryGetValue v.Stamp with
| true, limit -> limit
| _ ->
if IsValLocal env v then
......
......@@ -71,9 +71,9 @@ type Table<'T> =
tbl.rows.Add x
n
member tbl.FindOrAdd x =
let mutable res = Unchecked.defaultof<_>
let ok = tbl.tbl.TryGetValue(x, &res)
if ok then res else tbl.Add x
match tbl.tbl.TryGetValue x with
| true, res -> res
| _ -> tbl.Add x
static member Create n =
......
......@@ -870,9 +870,9 @@ type public TcGlobals(compilingFslib: bool, ilg:ILGlobals, fslibCcu: CcuThunk, d
TType_app (tcref, tinst)
else
let dict = getDecompileTypeDict()
let mutable builder = Unchecked.defaultof<_>
if dict.TryGetValue(tcref.Stamp, &builder) then builder tinst
else TType_app (tcref, tinst)
match dict.TryGetValue tcref.Stamp with
| true, builder -> builder tinst
| _ -> TType_app (tcref, tinst)
/// For cosmetic purposes "improve" some .NET types, e.g. Int32 --> int32.
/// Doing this normalization is a fairly performance critical piece of code as it is frequently invoked
......@@ -880,14 +880,14 @@ type public TcGlobals(compilingFslib: bool, ilg:ILGlobals, fslibCcu: CcuThunk, d
let improveTy (tcref: EntityRef) tinst =
if compilingFslib then
let dict = getBetterTypeDict1()
let mutable builder = Unchecked.defaultof<_>
if dict.TryGetValue(tcref.LogicalName, &builder) then builder tcref tinst
else TType_app (tcref, tinst)
match dict.TryGetValue tcref.LogicalName with
| true, builder -> builder tcref tinst
| _ -> TType_app (tcref, tinst)
else
let dict = getBetterTypeDict2()
let mutable builder = Unchecked.defaultof<_>
if dict.TryGetValue(tcref.Stamp, &builder) then builder tinst
else TType_app (tcref, tinst)
match dict.TryGetValue tcref.Stamp with
| true, builder -> builder tinst
| _ -> TType_app (tcref, tinst)
override x.ToString() = "<TcGlobals>"
......
......@@ -12394,8 +12394,8 @@ module TcRecdUnionAndEnumDeclarations = begin
let ValidateFieldNames (synFields: SynField list, tastFields: RecdField list) =
let seen = Dictionary()
for (sf, f) in List.zip synFields tastFields do
let mutable synField = Unchecked.defaultof<_>
if seen.TryGetValue(f.Name, &synField) then
match seen.TryGetValue f.Name with
| true, synField ->
match sf, synField with
| Field(_, _, Some id, _, _, _, _, _), Field(_, _, Some(_), _, _, _, _, _) ->
error(Error(FSComp.SR.tcFieldNameIsUsedModeThanOnce(id.idText), id.idRange))
......@@ -12403,7 +12403,7 @@ module TcRecdUnionAndEnumDeclarations = begin
| Field(_, _, None, _, _, _, _, _), Field(_, _, Some id, _, _, _, _, _) ->
error(Error(FSComp.SR.tcFieldNameConflictsWithGeneratedNameForAnonymousField(id.idText), id.idRange))
| _ -> assert false
else
| _ ->
seen.Add(f.Name, sf)
let TcUnionCaseDecl cenv env parent thisTy tpenv (UnionCase (synAttrs, id, args, xmldoc, vis, m)) =
......
......@@ -1887,7 +1887,7 @@ type internal FsiInteractionProcessor
if tcConfig.shadowCopyReferences then
let resolvedPath = ar.resolvedPath.ToUpperInvariant()
let fileTime = File.GetLastWriteTimeUtc(resolvedPath)
match referencedAssemblies.TryGetValue(resolvedPath) with
match referencedAssemblies.TryGetValue resolvedPath with
| false, _ ->
referencedAssemblies.Add(resolvedPath, fileTime)
FSIstrings.SR.fsiDidAHashr(ar.resolvedPath)
......
......@@ -134,7 +134,7 @@ let ImportILTypeRefUncached (env: ImportMap) m (tref: ILTypeRef) =
/// Import a reference to a type definition, given an AbstractIL ILTypeRef, with caching
let ImportILTypeRef (env: ImportMap) m (tref: ILTypeRef) =
match env.ILTypeRefToTyconRefCache.TryGetValue(tref) with
match env.ILTypeRefToTyconRefCache.TryGetValue tref with
| true, tcref -> tcref
| _ ->
let tcref = ImportILTypeRefUncached env m tref
......@@ -436,7 +436,7 @@ let multisetDiscriminateAndMap nodef tipf (items: ('Key list * 'Value) list) =
| [] -> ()
| key :: rest ->
buckets.[key] <-
match buckets.TryGetValue(key) with
match buckets.TryGetValue key with
| true, b -> (rest, v) :: b
| _ -> (rest, v) :: []
......
......@@ -43,12 +43,13 @@ type LightSyntaxStatus(initial:bool,warn:bool) =
type LexResourceManager() =
let strings = new System.Collections.Generic.Dictionary<string, Parser.token>(1024)
member x.InternIdentifierToken(s) =
let mutable res = Unchecked.defaultof<_>
let ok = strings.TryGetValue(s, &res)
if ok then res else
let res = IDENT s
(strings.[s] <- res; res)
match strings.TryGetValue s with
| true, res -> res
| _ ->
let res = IDENT s
strings.[s] <- res
res
/// Lexer parameters
type lexargs =
{ defines: string list
......
......@@ -146,13 +146,13 @@ type FileIndexTable() =
//
// TO move forward we should eventually introduce a new type NormalizedFileName that tracks this invariant.
member t.FileToIndex normalize filePath =
match fileToIndexTable.TryGetValue(filePath) with
match fileToIndexTable.TryGetValue filePath with
| true, idx -> idx
| _ ->
// Try again looking for a normalized entry.
let normalizedFilePath = if normalize then normalizeFilePath filePath else filePath
match fileToIndexTable.TryGetValue(normalizedFilePath) with
match fileToIndexTable.TryGetValue normalizedFilePath with
| true, idx ->
// Record the non-normalized entry if necessary
if filePath <> normalizedFilePath then
......
......@@ -587,9 +587,9 @@ let emitDataSwitch ilg (cg: ICodeGen<'Mark>) (avoidHelpers, cuspec, cases) =
for (i,case) in cases do dict.[i] <- case
let failLab = cg.GenerateDelayMark ()
let emitCase i _ =
let mutable res = Unchecked.defaultof<_>
let ok = dict.TryGetValue(i, &res)
if ok then res else cg.CodeLabel failLab
match dict.TryGetValue i with
| true, res -> res
| _ -> cg.CodeLabel failLab
let dests = Array.mapi emitCase cuspec.AlternativesArray
cg.EmitInstrs (mkGetTag ilg cuspec)
......
......@@ -22,15 +22,15 @@ type internal HashMultiMap<'Key,'Value>(n: int, hasheq: IEqualityComparer<'Key>)
then seq |> Seq.iter (fun (k,v) -> x.Add(k,v))
member x.GetRest(k) =
let mutable res = []
let ok = rest.TryGetValue(k,&res)
if ok then res else []
match rest.TryGetValue k with
| true, res -> res
| _ -> []
member x.Add(y,z) =
let mutable res = Unchecked.defaultof<'Value>
let ok = firstEntries.TryGetValue(y,&res)
if ok then
match firstEntries.TryGetValue y with
| true, res ->
rest.[y] <- res :: x.GetRest(y)
| _ -> ()
firstEntries.[y] <- z
member x.Clear() =
......@@ -52,16 +52,16 @@ type internal HashMultiMap<'Key,'Value>(n: int, hasheq: IEqualityComparer<'Key>)
member x.Item
with get(y : 'Key) =
let mutable res = Unchecked.defaultof<'Value>
let ok = firstEntries.TryGetValue(y,&res)
if ok then res else raise (KeyNotFoundException("The item was not found in collection"))
match firstEntries.TryGetValue y with
| true, res -> res
| _ -> raise (KeyNotFoundException("The item was not found in collection"))
and set (y:'Key) (z:'Value) =
x.Replace(y,z)
member x.FindAll(y) =
let mutable res = Unchecked.defaultof<'Value>
let ok = firstEntries.TryGetValue(y,&res)
if ok then res :: x.GetRest(y) else []
match firstEntries.TryGetValue y with
| true, res -> res :: x.GetRest(y)
| _ -> []
member x.Fold f acc =
let mutable res = acc
......@@ -88,14 +88,12 @@ type internal HashMultiMap<'Key,'Value>(n: int, hasheq: IEqualityComparer<'Key>)
member x.ContainsKey(y) = firstEntries.ContainsKey(y)
member x.Remove(y) =
let mutable res = Unchecked.defaultof<'Value>
let ok = firstEntries.TryGetValue(y,&res)
match firstEntries.TryGetValue y with
// NOTE: If not ok then nothing to remove - nop
if ok then
| true, _res ->
// We drop the FirstEntry. Here we compute the new FirstEntry and residue MoreEntries
let mutable res = []
let ok = rest.TryGetValue(y,&res)
if ok then
match rest.TryGetValue y with
| true, res ->
match res with
| [h] ->
firstEntries.[y] <- h;
......@@ -105,16 +103,17 @@ type internal HashMultiMap<'Key,'Value>(n: int, hasheq: IEqualityComparer<'Key>)
rest.[y] <- t
| _ ->
()
else
| _ ->
firstEntries.Remove(y) |> ignore
| _ -> ()
member x.Replace(y,z) =
firstEntries.[y] <- z
member x.TryFind(y) =
let mutable res = Unchecked.defaultof<'Value>
let ok = firstEntries.TryGetValue(y,&res)
if ok then Some(res) else None
match firstEntries.TryGetValue y with
| true, res -> Some res
| _ -> None
member x.Count = firstEntries.Count
......
......@@ -369,7 +369,7 @@ module internal ToolLocationHelper =
// Doesn't need to be virtual @@@@@
abstract member GetPathToDotNetFramework: DotNetFrameworkArchitecture -> string
default this.GetPathToDotNetFramework arch =
match this.pathsToDotNetFramework.TryGetValue(arch) with
match this.pathsToDotNetFramework.TryGetValue arch with
| true, x -> x
| _ ->
if not (CheckForFrameworkInstallation this.dotNetFrameworkRegistryKey this.dotNetFrameworkSetupRegistryInstalledName) then null
......@@ -766,7 +766,7 @@ module internal ToolLocationHelper =
array.ToDictionary<DotNetFrameworkSpec, Version>(fun spec -> spec.Version)
let getDotNetFrameworkSpec version =
match dotNetFrameworkSpecDict.TryGetValue(version) with
match dotNetFrameworkSpecDict.TryGetValue version with
| true, x -> x
| _ -> raise (getArgumentException version)
......
......@@ -35,7 +35,7 @@ let B = File1.A + File1.A"""
interface IFileSystem with
// Implement the service to open files for reading and writing
member __.FileStreamReadShim(fileName) =
match files.TryGetValue(fileName) with
match files.TryGetValue fileName with
| true, text -> new MemoryStream(Encoding.UTF8.GetBytes(text)) :> Stream
| _ -> defaultFileSystem.FileStreamReadShim(fileName)
......@@ -49,7 +49,7 @@ let B = File1.A + File1.A"""
defaultFileSystem.FileStreamWriteExistingShim(fileName)
member __.ReadAllBytesShim(fileName) =
match files.TryGetValue(fileName) with
match files.TryGetValue fileName with
| true, text -> Encoding.UTF8.GetBytes(text)
| _ -> defaultFileSystem.ReadAllBytesShim(fileName)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册