Skip to content

Completion type inference in foreach item infers from the first declaration in a document rather than the iterated collection #24205

@jul-m

Description

@jul-m

Prerequisites

Steps to reproduce

$script = 'function Test-Function {
    [hashtable] $var = @{ Completion = "error" }
}

[datetime[]] $myList = @((Get-Date),(Get-Date))

foreach ($good in $myList) {
    # "$good." suggestions match with "datetime" type well
}

foreach ($var in $myList) {
    # "$var." suggestions match with "hashtable" type declared out of scope rather than "datetime"
    $var.'

TabExpansion2 -inputScript $script -cursorColumn $script.Length | % CompletionMatches

Expected behavior

CompletionText        ListItemText         ResultType ToolTip
--------------        ------------         ---------- -------
Date                  Date                   Property datetime Date {get;}
Day                   Day                    Property int Day {get;}
DayOfWeek             DayOfWeek              Property System.DayOfWeek DayOfWeek {get;}
DayOfYear             DayOfYear              Property int DayOfYear {get;}
Hour                  Hour                   Property int Hour {get;}
Kind                  Kind                   Property System.DateTimeKind Kind {get;}
Microsecond           Microsecond            Property int Microsecond {get;}
Millisecond           Millisecond            Property int Millisecond {get;}
Minute                Minute                 Property int Minute {get;}
Month                 Month                  Property int Month {get;}
Nanosecond            Nanosecond             Property int Nanosecond {get;}
Second                Second                 Property int Second {get;}
Ticks                 Ticks                  Property long Ticks {get;}
TimeOfDay             TimeOfDay              Property timespan TimeOfDay {get;}
Year                  Year                   Property int Year {get;}
DateTime              DateTime               Property System.Object DateTime {get=if ((& { Set-StrictMode -Version 1; $this.DisplayHint }) -ieq  "Date")…
Add(                  Add                      Method datetime Add(timespan value)
AddDays(              AddDays                  Method datetime AddDays(double value)
AddHours(             AddHours                 Method datetime AddHours(double value)
AddMicroseconds(      AddMicroseconds          Method datetime AddMicroseconds(double value)
AddMilliseconds(      AddMilliseconds          Method datetime AddMilliseconds(double value)
AddMinutes(           AddMinutes               Method datetime AddMinutes(double value)
AddMonths(            AddMonths                Method datetime AddMonths(int months)
AddSeconds(           AddSeconds               Method datetime AddSeconds(double value)
AddTicks(             AddTicks                 Method datetime AddTicks(long value)
AddYears(             AddYears                 Method datetime AddYears(int value)
CompareTo(            CompareTo                Method int CompareTo(System.Object value)…
Deconstruct(          Deconstruct              Method void Deconstruct([ref] System.DateOnly date, [ref] System.TimeOnly time)…
Equals(               Equals                   Method bool Equals(System.Object value)…
GetDateTimeFormats(   GetDateTimeFormats       Method string[] GetDateTimeFormats()…
GetHashCode(          GetHashCode              Method int GetHashCode()
GetObjectData(        GetObjectData            Method void ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
GetType(              GetType                  Method type GetType()
GetTypeCode(          GetTypeCode              Method System.TypeCode GetTypeCode()…
IsDaylightSavingTime( IsDaylightSavingTime     Method bool IsDaylightSavingTime()
Subtract(             Subtract                 Method timespan Subtract(datetime value)…
ToBinary(             ToBinary                 Method long ToBinary()
ToBoolean(            ToBoolean                Method bool IConvertible.ToBoolean(System.IFormatProvider provider)
ToByte(               ToByte                   Method byte IConvertible.ToByte(System.IFormatProvider provider)
ToChar(               ToChar                   Method char IConvertible.ToChar(System.IFormatProvider provider)
ToDateTime(           ToDateTime               Method datetime IConvertible.ToDateTime(System.IFormatProvider provider)
ToDecimal(            ToDecimal                Method decimal IConvertible.ToDecimal(System.IFormatProvider provider)
ToDouble(             ToDouble                 Method double IConvertible.ToDouble(System.IFormatProvider provider)
ToFileTime(           ToFileTime               Method long ToFileTime()
ToFileTimeUtc(        ToFileTimeUtc            Method long ToFileTimeUtc()
ToInt16(              ToInt16                  Method short IConvertible.ToInt16(System.IFormatProvider provider)
ToInt32(              ToInt32                  Method int IConvertible.ToInt32(System.IFormatProvider provider)
ToInt64(              ToInt64                  Method long IConvertible.ToInt64(System.IFormatProvider provider)
ToLocalTime(          ToLocalTime              Method datetime ToLocalTime()
ToLongDateString(     ToLongDateString         Method string ToLongDateString()
ToLongTimeString(     ToLongTimeString         Method string ToLongTimeString()
ToOADate(             ToOADate                 Method double ToOADate()
ToSByte(              ToSByte                  Method sbyte IConvertible.ToSByte(System.IFormatProvider provider)
ToShortDateString(    ToShortDateString        Method string ToShortDateString()
ToShortTimeString(    ToShortTimeString        Method string ToShortTimeString()
ToSingle(             ToSingle                 Method float IConvertible.ToSingle(System.IFormatProvider provider)
ToString(             ToString                 Method string ToString()…
ToType(               ToType                   Method System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)
ToUInt16(             ToUInt16                 Method ushort IConvertible.ToUInt16(System.IFormatProvider provider)
ToUInt32(             ToUInt32                 Method uint IConvertible.ToUInt32(System.IFormatProvider provider)
ToUInt64(             ToUInt64                 Method ulong IConvertible.ToUInt64(System.IFormatProvider provider)
ToUniversalTime(      ToUniversalTime          Method datetime ToUniversalTime()
TryFormat(            TryFormat                Method bool TryFormat(System.Span[char] destination, [ref] int charsWritten, System.ReadOnlySpan[char] format = default, System.IFormatProvider provider =…

Actual behavior

CompletionText     ListItemText      ResultType ToolTip
--------------     ------------      ---------- -------
Count              Count               Property int Count { get; }
IsFixedSize        IsFixedSize         Property bool IsFixedSize { get; }
IsReadOnly         IsReadOnly          Property bool IsReadOnly { get; }
IsSynchronized     IsSynchronized      Property bool IsSynchronized { get; }
Keys               Keys                Property System.Collections.ICollection Keys { get; }
SyncRoot           SyncRoot            Property System.Object SyncRoot { get; }
Values             Values              Property System.Collections.ICollection Values { get; }
Add(               Add                   Method void Add(System.Object key, System.Object value)…
Clear(             Clear                 Method void Clear()…
Clone(             Clone                 Method System.Object Clone()…
Contains(          Contains              Method bool Contains(System.Object key)…
ContainsKey(       ContainsKey           Method bool ContainsKey(System.Object key)
ContainsValue(     ContainsValue         Method bool ContainsValue(System.Object value)
CopyTo(            CopyTo                Method void CopyTo(array array, int arrayIndex)…
Equals(            Equals                Method bool Equals(System.Object obj)
GetEnumerator(     GetEnumerator         Method System.Collections.IDictionaryEnumerator GetEnumerator()…
GetHashCode(       GetHashCode           Method int GetHashCode()
GetObjectData(     GetObjectData         Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)…
GetType(           GetType               Method type GetType()
OnDeserialization( OnDeserialization     Method void OnDeserialization(System.Object sender)…
Remove(            Remove                Method void Remove(System.Object key)…
ToString(          ToString              Method string ToString()

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.5
PSEdition                      Core
GitCommitId                    7.4.5
OS                             Darwin 23.5.0 Darwin Kernel Version 23.5.0: Wed May  1 20:19:05 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8112
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIndicates that a PR is out for the issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions