Skip to content

Commit 8f982c5

Browse files
authored
Merge pull request #6 from apiiro/ohad/fix-generics-stack-overflow
Fixed stack overflow with generics.
2 parents 21ca8a7 + 385cace commit 8f982c5

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Analysis/Ast/Impl/Specializations/Typing/Types/GenericType.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,15 @@ public IMember CreateInstance(IArgumentSet args) {
128128
throw new ArgumentException(@"Generic type instance construction arguments must be all of IPythonType", nameof(args));
129129
}
130130
var specific = CreateSpecificType(args);
131-
return specific == null
131+
return specific == null || IsSameType(specific)
132132
? DeclaringModule.Interpreter.UnknownType.CreateInstance(args)
133133
: specific.CreateInstance(args);
134134
}
135135

136+
private bool IsSameType(IPythonType specific)
137+
=> specific.MemberType == PythonMemberType.Generic &&
138+
this.Name == specific.Name;
139+
136140
public IMember Call(IPythonInstance instance, string memberName, IArgumentSet args) => DeclaringModule.Interpreter.UnknownType;
137141
public IMember Index(IPythonInstance instance, IArgumentSet args) => DeclaringModule.Interpreter.UnknownType;
138142

src/Analysis/Ast/Test/GenericsTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// See the Apache Version 2.0 License for specific language governing
1414
// permissions and limitations under the License.
1515

16+
using System;
1617
using System.Linq;
1718
using System.Threading.Tasks;
1819
using FluentAssertions;
@@ -1412,6 +1413,23 @@ import pathlib
14121413
analysis.Should().HaveVariable("root").OfType("Path");
14131414
analysis.Should().HaveVariable("subdir").OfType("PurePath");
14141415
analysis.Should().HaveVariable("child").OfType("PurePath");
1416+
}
1417+
1418+
[TestMethod, Priority(0)]
1419+
public async Task GenericStackOverflowBub() {
1420+
/*
1421+
* Apiiro fix.
1422+
* The code below created stack overflow if SpecializedGenericType.CreateInstance
1423+
* Just checking we are not crushing here.
1424+
*/
1425+
const string code = @"
1426+
from typing import Union, Sequence, TypeVar
1427+
_T = TypeVar(""_T"")
1428+
_NestedSequence = Sequence[Sequence[_T]]
1429+
_ArrayLike = _NestedSequence[_T]
1430+
";
1431+
await GetAnalysisAsync(code, PythonVersions.Python37);
1432+
Assert.IsTrue(true);
14151433
}
14161434
}
14171435
}

0 commit comments

Comments
 (0)