From 5ccbb97ae3f575732b0a3ba3e9a61b2317c57005 Mon Sep 17 00:00:00 2001 From: MartinGC94 <42123497+MartinGC94@users.noreply.github.com> Date: Mon, 25 Jul 2022 20:15:19 +0200 Subject: [PATCH] Fix type inference for `ICollection` (#17752) --- .../engine/parser/TypeInferenceVisitor.cs | 3 ++- test/powershell/engine/Api/TypeInference.Tests.ps1 | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs b/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs index 363ce3536d..3ace7fb3a0 100644 --- a/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs +++ b/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs @@ -2443,9 +2443,10 @@ namespace System.Management.Automation foreach (var m in type.GetMethods(BindingFlags.Public | BindingFlags.Instance)) { var name = m.Name; + // Equals without string allocation if (name.Length == propertyName.Length + 4 && name.StartsWith("get_") - && propertyName.IndexOf(name, 4, StringComparison.Ordinal) == 4) + && name.IndexOf(propertyName, 4, StringComparison.Ordinal) == 4) { res.Add(m); } diff --git a/test/powershell/engine/Api/TypeInference.Tests.ps1 b/test/powershell/engine/Api/TypeInference.Tests.ps1 index b5f2f64567..fbcc784570 100644 --- a/test/powershell/engine/Api/TypeInference.Tests.ps1 +++ b/test/powershell/engine/Api/TypeInference.Tests.ps1 @@ -1251,6 +1251,12 @@ Describe "Type inference Tests" -tags "CI" { $res.Count | Should -Be 1 $res.Name | Should -Be 'System.Int32[]' } + + It 'Infers type of index item in an ICollection' -Skip:(!$IsWindows) { + $res = [AstTypeInference]::InferTypeOf( { (Get-Acl -Path C:\).Access[0] }.Ast) + $res.Count | Should -Be 1 + $res.Name | Should -Be 'System.Security.AccessControl.AuthorizationRule' + } } Describe "AstTypeInference tests" -Tags CI {