Skip to content
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

GenericParam type breaks Dictionary.TryAdd #2124

Closed
disarray2077 opened this issue Feb 1, 2025 · 1 comment
Closed

GenericParam type breaks Dictionary.TryAdd #2124

disarray2077 opened this issue Feb 1, 2025 · 1 comment

Comments

@disarray2077
Copy link
Contributor

disarray2077 commented Feb 1, 2025

I wasn't entirely sure if this was worth reporting, as the issue can be avoided by simply returning early when typeof(T).IsGenericParam is true. However, it took me some time (and moments of confusion) to figure this out, so I figured I should report it anyway.

The issue manifests in two ways:

  1. Attempting to assign through the valuePtr pointer results in the error:

    Cannot assign to read-only local variable 'valuePtr'

  2. Trying to read valuePtr from the else branch results in the error:

    Identifier not found

using System;
using System.Collections;
using System.Reflection;
using System.Diagnostics;

namespace simple_test;

namespace TypeTraits
{
	static
	{
		struct Yes;
		struct No;
	
		struct IsIndexable<T, TKey>
		{
			public typealias Result = comptype(_isIndexable(typeof(T), typeof(TKey)));
	
			private class IndexerProperty
			{
			}
	
			[Comptime]
			private static Type _isIndexable(Type type, Type keyType)
			{
				Dictionary<StringView, IndexerProperty> indexers = scope .();
				if (indexers.TryAdd("foo", let keyPtr, let valuePtr))
				{
					*valuePtr = null; // Cannot assign to read-only local variable 'valuePtr'
				}
				else
				{
					IndexerProperty indexerProp = (.)*valuePtr; // Identifier not found
					Debug.WriteLine(indexerProp.ToString(.. scope .()));
				}
	
				return typeof(No);
			}
		}
	}
}

Tested with: 05cda98

@bfiete
Copy link
Collaborator

bfiete commented Feb 1, 2025

Fixed at cce1611

The comptime will still crash with a null pointer at (.)*valuePtr if you don't have the IsGenericParam test, but at least the comptime method will actually generate without error now.

@bfiete bfiete closed this as completed Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants