Skip to content

[generator] Fix invalid parsing of complex generic types. #729

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

Merged
merged 1 commit into from
Sep 25, 2020

Conversation

jpobst
Copy link
Contributor

@jpobst jpobst commented Sep 24, 2020

Fixes: #728

In order to prevent namespace clashes we always write non-intrinsic types prefixed with global::.

For example:

System.Collections.Generic.List<Java.Lang.Object>

becomes

global::System.Collections.Generic.List<global::Java.Lang.Object>

To do this, we have to parse a type into its component types so we can add global:: to each part. However our parsing algorithm struggles with generic types with multiple type arguments.

Thus:

System.Collections.Generic.IDictionary<System.Collections.Generic.IList<string>, Kotlin.Pair>

becomes

global::System.Collections.Generic.IDictionary<global::System.Collections.Generic.IList<string>>

and

System.Collections.Generic.IDictionary<System.Collections.Generic.IList<string>, System.Collections.Generic.IList<Kotlin.Pair>>

throws an ArgumentOutOfRangeException.

This PR introduces a new recursive parser that better understands generic types and generates the expected outputs.

@jpobst jpobst marked this pull request as ready for review September 24, 2020 20:50
Assert.AreEqual ("global::System.Collections.Generic.IDictionary<global::System.Collections.Generic.IList<string>, global::Kotlin.Pair>",
opt.GetOutputName ("System.Collections.Generic.IDictionary<System.Collections.Generic.IList<string>, Kotlin.Pair>"));

Assert.AreEqual ("global::System.Collections.Generic.IDictionary<global::System.Collections.Generic.IList<string>, global::System.Collections.Generic.IList<global::Kotlin.Pair>>",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all "positive" tests.

You should also test "negative" tests, "invalid" types. What happens if it attempts to parse System.Collections.Generic.List<> (missing type) or System.Collections.Generic.List<<> (too many <s) or System.Collections.Generic.List<>> (too many >s)?

Presumably it fails; does it? Does it fail in the way we expect? (Or does it instead return an empty string?)

Copy link
Member

@jonpryor jonpryor Sep 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also try nested generic types, a'la

List<List<string>.Enumerator[]>.

Assert.AreEqual ("void", opt.GetOutputName ("void"));
Assert.AreEqual ("void", opt.GetOutputName ("System.Void"));
Assert.AreEqual ("params int[]", opt.GetOutputName ("params int[]"));
Assert.AreEqual ("params global::System.Object[]", opt.GetOutputName ("params System.Object[]"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also check arrays-of-arrays, e.g. int[][][].

opt.GetOutputName ("System.Collections.Generic.Dictionary<string, string>"));

Assert.AreEqual ("global::System.Collections.Generic.List<global::System.Collections.Generic.List<string>>",
opt.GetOutputName ("System.Collections.Generic.List<System.Collections.Generic.List<string>>"));
Copy link
Member

@jonpryor jonpryor Sep 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also try intermixing arrays & generics, e.g. System.Collections.Generic.List<System.Collections.Generic.List<string>[]>[].

@jpobst jpobst force-pushed the nested-generic-types branch from 2d8bfe9 to 2ba8bd7 Compare September 25, 2020 14:45
@jpobst
Copy link
Contributor Author

jpobst commented Sep 25, 2020

Thanks for the additional test cases, they revealed a few more issues. Granted, they didn't work previously so it wouldn't have been a regression, but now we handle those cases as well!

The behavior with invalid types is "undefined". It may return an invalid type string or it may throw an ArgumentOutOfRangeException depending on how it errors. It's not really possible to reject invalid input unless we write a validating type parser that fully implements the type spec.

Luckily at this point we are simply outputting types that have already been validated by other aspects of generator so we shouldn't be seeing invalid types.

@jonpryor jonpryor merged commit 8b1b050 into master Sep 25, 2020
@jonpryor jonpryor deleted the nested-generic-types branch September 25, 2020 22:39
@jpobst jpobst added this to the 11.1 (16.9 / 8.9) milestone Oct 13, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to parse generic types with generic type arguments
2 participants