-
Notifications
You must be signed in to change notification settings - Fork 4k
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
CS0021 error (Cannot apply indexing with [] to an expression of type) appears incorrectly on code that compiles #69663
Comments
@dotnet/roslyn-compiler Code that fails is attached to the original azdo bug. |
User code: File 1: namespace Aiml.Tags;
internal class Program {
public static void TestMethod(Tuple tuple) => _ = tuple[""];
} File 2: using System.Collections;
namespace Aiml;
/// <summary>Represents a set of variable bindings in the process of resolving a <see cref="Tags.Select"/> query.</summary>
/// <remarks>This class is represented as a singly-linked list.</remarks>
public class Tuple(string key, string value, Tuple? next) : IEnumerable<KeyValuePair<string, string>> {
public string Key { get; } = key;
public string Value { get; } = value;
public Tuple? Next { get; } = next;
public Tuple(string key, string value) : this(key, value, null) { }
public string? this[string key] {
get {
var tuple = this;
do {
if (key.Equals(tuple.Key, StringComparison.OrdinalIgnoreCase)) return tuple.Value;
tuple = tuple.Next;
} while (tuple != null);
return null;
}
}
/// <summary>Returns an encoded string containing the contents of the specified variables. The encoded string shall not contain spaces.</summary>
public string Encode(IReadOnlyCollection<string>? visibleVars) {
using var ms = new MemoryStream();
using var writer = new BinaryWriter(ms);
foreach (var e in this) {
if (visibleVars is not null && !visibleVars.Contains(e.Key)) continue;
writer.Write(e.Key);
writer.Write(e.Value);
}
return Convert.ToBase64String(ms.GetBuffer(), 0, (int) ms.Position);
}
/// <summary>Returns the value of the specified variable from an encoded string, or <see langword="null"/> if the variable is not bound in the encoded string.</summary>
public static string? GetFromEncoded(string encoded, string key) {
var array = Convert.FromBase64String(encoded);
using var ms = new MemoryStream(array);
using var reader = new BinaryReader(ms);
while (ms.Position < ms.Length) {
var key2 = reader.ReadString();
var value = reader.ReadString();
if (key.Equals(key2, StringComparison.OrdinalIgnoreCase)) return value;
}
return null;
}
public IEnumerator<KeyValuePair<string, string>> GetEnumerator() {
var tuple = this;
do {
yield return new(tuple.Key, tuple.Value);
tuple = tuple.Next;
} while (tuple != null);
}
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
} |
I was not able to repro with the given code. The
I'm a bit confused by this observation. If there is a CS0021, then the program should not "successfully compile" since there's an error. Do you mean that there's a difference between what the IDE and the Build report? |
There is indeed a difference between what the IDE and build report. No such error appears in the MSBuild output. I've reproduced this in a sample project created using
|
@AndrioCelos Could you share that small repro project? I'll try it again.
Thanks. That's useful to narrow down why you can repro but I couldn't. In my case, VS/Intellisense shows: |
It's available now at https://gist.github.com/AndrioCelos/1264f6a36ae9f9d96e76f7330bc8bbdc.
Also, as an update to my earlier comment, the version of Roslyn used in my Visual Studio setup produced this:
|
This test reproduces the issue: [Fact]
public void Indexer_SymbolInfo()
{
var source = """
C c = null;
_ = c[2];
class C(int p)
{
public int this[int i] => p;
}
""";
var comp = CreateCompilation(source);
var tree = comp.SyntaxTrees.Single();
var indexer = tree.GetRoot().DescendantNodes().OfType<ElementAccessExpressionSyntax>().Single();
Assert.Equal("c[2]", indexer.ToString());
var model = comp.GetSemanticModel(tree);
var info = model.GetSymbolInfo(indexer);
Assert.NotNull(info.Symbol);
} Seems the indexer is not found due to this check which I'm not sure why it's there: roslyn/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol.cs Line 3388 in cc0bd7d
This doesn't happen during normal compilation because this code is only executed if searching for the symbol when members are not yet completed. |
Thanks much @jjonescz |
This issue has been moved from a ticket on Developer Community.
[jcouv update:] Here's the internal azdo issue.
[severity:It's more difficult to complete my work]
I have noticed that in some situations, the error CS0021 appears on a call to an indexer declared in a type in my own code that appears to be valid and successfully compiles.
The issue seems to be related to the fact that the type in question has a primary constructor, because removing it causes the error to disappear.
A sample project where the issue is occuring is attached (line 3 of Program.cs).
The same issue occurs when using the C# extension for Visual Studio Code.
Original Comments
Feedback Bot on 8/21/2023, 07:26 PM:
(private comment, text removed)
Feedback Bot on 8/22/2023, 06:02 AM:
(private comment, text removed)
Original Solutions
(no solutions)
The text was updated successfully, but these errors were encountered: