-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Replace span_look_ahead with span_followed_by #154745
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
Open
chenyukang
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
chenyukang:yukang-fix-span-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1987,10 +1987,23 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { | |
| // where a brace being opened means a block is being started. Look | ||
| // ahead for the next text to see if `span` is followed by a `{`. | ||
| let sm = self.r.tcx.sess.source_map(); | ||
| if let Some(followed_brace_span) = sm.span_look_ahead(span, "{", Some(50)) { | ||
| if let Some(open_brace_span) = sm.span_followed_by(span, "{") { | ||
| // In case this could be a struct literal that needs to be surrounded | ||
| // by parentheses, find the appropriate span. | ||
| let close_brace_span = sm.span_look_ahead(followed_brace_span, "}", Some(50)); | ||
| let close_brace_span = | ||
| sm.span_to_next_source(open_brace_span).ok().and_then(|next_source| { | ||
| let offset = next_source.find('}')?; | ||
| if next_source[..offset].chars().count() >= 50 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment explaining what this |
||
| return None; | ||
| } | ||
| let start = open_brace_span.hi() + rustc_span::BytePos(offset as u32); | ||
| Some(Span::new( | ||
| start, | ||
| start + rustc_span::BytePos(1_u32), | ||
| open_brace_span.ctxt(), | ||
| None, | ||
| )) | ||
| }); | ||
| let closing_brace = close_brace_span.map(|sp| span.to(sp)); | ||
| (true, closing_brace) | ||
| } else { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #[derive(PartialEq)] | ||
| struct T { pub x: i32 } | ||
|
|
||
| #[derive(PartialEq)] | ||
| struct U { } | ||
|
|
||
| fn main() { | ||
| // Parser will report an error here | ||
| if T { x: 10 } == T {} {} | ||
| //~^ ERROR struct literals are not allowed here | ||
| //~| ERROR expected value, found struct `T` | ||
|
|
||
| // Regression test for the `followed_by_brace` helper: | ||
| // comments inside the braces should not suppress the parenthesized struct literal suggestion. | ||
| if U { /* keep comment here */ } == U {} | ||
| //~^ ERROR E0423 | ||
| //~| ERROR expected expression, found `==` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| error: struct literals are not allowed here | ||
| --> $DIR/E0423-struct-literal-comment.rs:9:8 | ||
| | | ||
| LL | if T { x: 10 } == T {} {} | ||
| | ^^^^^^^^^^^ | ||
| | | ||
| help: surround the struct literal with parentheses | ||
| | | ||
| LL | if (T { x: 10 }) == T {} {} | ||
| | + + | ||
|
|
||
| error: expected expression, found `==` | ||
| --> $DIR/E0423-struct-literal-comment.rs:15:38 | ||
| | | ||
| LL | if U { /* keep comment here */ } == U {} | ||
| | ^^ expected expression | ||
|
|
||
| error[E0423]: expected value, found struct `T` | ||
| --> $DIR/E0423-struct-literal-comment.rs:9:23 | ||
| | | ||
| LL | if T { x: 10 } == T {} {} | ||
| | ^ not a value | ||
| | | ||
| help: surround the struct literal with parentheses | ||
| | | ||
| LL | if T { x: 10 } == (T {}) {} | ||
| | + + | ||
|
|
||
| error[E0423]: expected value, found struct `U` | ||
| --> $DIR/E0423-struct-literal-comment.rs:15:8 | ||
| | | ||
| LL | if U { /* keep comment here */ } == U {} | ||
| | ^ not a value | ||
| | | ||
| help: surround the struct literal with parentheses | ||
| | | ||
| LL | if (U { /* keep comment here */ }) == U {} | ||
| | + + | ||
|
|
||
| error: aborting due to 4 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0423`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can not use
span_look_aheadhere, because we only want to make sure there is a}in 50 range.otherwise we can not handle the scenario like
U { /* keep comment here */ }, since there are some non whitespace chars inside{ }.https://github.com/rust-lang/rust/pull/154745/changes#diff-68dd28577bfdbdba4a4c4ee9d20797f2f0e3939d926d4bdcbe13b1117a2dbe32R35-R38 is the test case for this.