Skip to content

Commit 97445ba

Browse files
authored
Fix SubText ctor parameter verification. (#78989)
* Fix SubText ctor parameter verification. This ctor currently throws given a zero length span at the end (and only the end) of the subtext. This evidently became far more prevalent with this (fairly) recent change to optimize newline information in our sourcetext classes. (#74728) It doesn't make to allow zero length spans at all locations but at the end of the subtext, so the fix is just to allow construction in that case too. Big props to Manish Vasani for providing a very actionable repro for this. Fixes #78985. Note there is a potentially related issue outlined in #76225 that looks attributable to the PR above too. It possible that this addresses that issue too, but I'm not completely convinced.
1 parent 52732de commit 97445ba

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/Compilers/Core/CodeAnalysisTest/Text/TextChangeTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ public void TestSubTextSpanMid()
5858
Assert.Equal("o W", subText.ToString());
5959
}
6060

61+
[Fact]
62+
public void TestSubTextSubTextStart()
63+
{
64+
var text = SourceText.From("Hello World");
65+
var subText = text.GetSubText(new TextSpan(0, 5));
66+
var subSubText = subText.GetSubText(new TextSpan(0, 0));
67+
68+
Assert.Equal("Hello", subText.ToString());
69+
Assert.Equal(0, subSubText.Length);
70+
}
71+
72+
[Fact]
73+
[WorkItem(78989, "https://github.com/dotnet/roslyn/pull/78989")]
74+
public void TestSubTextSubTextEnd()
75+
{
76+
var text = SourceText.From("Hello World");
77+
var subText = text.GetSubText(6);
78+
var subSubText = subText.GetSubText(subText.Length);
79+
80+
Assert.Equal("World", subText.ToString());
81+
Assert.Equal(0, subSubText.Length);
82+
}
83+
6184
[Fact]
6285
public void TestChangedText()
6386
{

src/Compilers/Core/Portable/Text/SubText.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public SubText(SourceText text, TextSpan span)
2222
throw new ArgumentNullException(nameof(text));
2323
}
2424

25+
// span.Start and span.End are valid wrt eachother by nature of being passed in as a TextSpan,
26+
// so there is no need to verify span.Start against text.Length or span.End against zero.
2527
if (span.Start < 0
26-
|| span.Start >= text.Length
27-
|| span.End < 0
2828
|| span.End > text.Length)
2929
{
3030
throw new ArgumentOutOfRangeException(nameof(span));

0 commit comments

Comments
 (0)