Skip to content

reduce the amount of panics in {TokenStream, Literal}::from_str calls#147859

Open
cyrgani wants to merge 1 commit intorust-lang:mainfrom
cyrgani:nonfatal-tokenstream-parse
Open

reduce the amount of panics in {TokenStream, Literal}::from_str calls#147859
cyrgani wants to merge 1 commit intorust-lang:mainfrom
cyrgani:nonfatal-tokenstream-parse

Conversation

@cyrgani
Copy link
Contributor

@cyrgani cyrgani commented Oct 18, 2025

Before this PR, calling TokenStream::from_str or Literal::from_str with an invalid argument would always cause a compile error, even if the TokenStream is not used afterwards at all.
This PR changes this so it returns a LexError instead in some cases.

This is very theoretically a breaking change, but the doc comment on the impl already says

/// NOTE: some errors may cause panics instead of returning `LexError`. We reserve the right to
/// change these errors into `LexError`s later.

Fixes some cases of #58736.

@rustbot
Copy link
Collaborator

rustbot commented Oct 18, 2025

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. labels Oct 18, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 18, 2025

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@cyrgani
Copy link
Contributor Author

cyrgani commented Oct 18, 2025

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 18, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 18, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@cyrgani cyrgani force-pushed the nonfatal-tokenstream-parse branch from e2c0009 to 80b67bb Compare October 19, 2025 09:03
@cyrgani
Copy link
Contributor Author

cyrgani commented Oct 19, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 19, 2025
@cyrgani cyrgani force-pushed the nonfatal-tokenstream-parse branch from ba098bc to 0274d78 Compare October 21, 2025 14:28
@cyrgani
Copy link
Contributor Author

cyrgani commented Oct 23, 2025

This does not fix #58736 entirely yet:

TokenStream::from_str("0b2");

is still a compile error because it internally calls literal_from_str, which then emits the diagnostic.

@davidtwco
Copy link
Member

I'm not familiar enough with this part of the compiler

r? compiler

@rustbot rustbot assigned wesleywiser and unassigned davidtwco Oct 25, 2025
@cyrgani cyrgani force-pushed the nonfatal-tokenstream-parse branch from 0274d78 to 13d3d06 Compare October 29, 2025 22:19
@rustbot

This comment has been minimized.

@cyrgani cyrgani changed the title replace TokenStream::from_str panics with LexErrors reduce the amount of panics in {TokenStream, Literal}::from_str calls Oct 29, 2025
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 17, 2025
add larger test for `proc_macro` `FromStr` implementations

Currently, there are only few tests that check the output of `TokenStream::from_str` and `Literal::from_str` (which is somewhat understandable as the rustc implementation just delegates these calls to the parser). In preparation for both the standalone backend (rust-lang#130856) which will probably need to reimplement this logic as well as for removing panics from these functions (rust-lang#58736), this PR adds a test which shows the various messy ways of how these functions report errors and the return values for successful parses.
Followup PRs such as rust-lang#147859 will change more and more of these "diagnostic + error"s into `LexErrors`.

The test structure with the extra module is used to allow reusing it later easily for the standalone backend.
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 17, 2025
add larger test for `proc_macro` `FromStr` implementations

Currently, there are only few tests that check the output of `TokenStream::from_str` and `Literal::from_str` (which is somewhat understandable as the rustc implementation just delegates these calls to the parser). In preparation for both the standalone backend (rust-lang#130856) which will probably need to reimplement this logic as well as for removing panics from these functions (rust-lang#58736), this PR adds a test which shows the various messy ways of how these functions report errors and the return values for successful parses.
Followup PRs such as rust-lang#147859 will change more and more of these "diagnostic + error"s into `LexErrors`.

The test structure with the extra module is used to allow reusing it later easily for the standalone backend.
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 17, 2025
add larger test for `proc_macro` `FromStr` implementations

Currently, there are only few tests that check the output of `TokenStream::from_str` and `Literal::from_str` (which is somewhat understandable as the rustc implementation just delegates these calls to the parser). In preparation for both the standalone backend (rust-lang#130856) which will probably need to reimplement this logic as well as for removing panics from these functions (rust-lang#58736), this PR adds a test which shows the various messy ways of how these functions report errors and the return values for successful parses.
Followup PRs such as rust-lang#147859 will change more and more of these "diagnostic + error"s into `LexErrors`.

The test structure with the extra module is used to allow reusing it later easily for the standalone backend.
JaclynCodes added a commit to JaclynCodes/rust that referenced this pull request Nov 17, 2025
add larger test for `proc_macro` `FromStr` implementations

Currently, there are only few tests that check the output of `TokenStream::from_str` and `Literal::from_str` (which is somewhat understandable as the rustc implementation just delegates these calls to the parser). In preparation for both the standalone backend (rust-lang#130856) which will probably need to reimplement this logic as well as for removing panics from these functions (rust-lang#58736), this PR adds a test which shows the various messy ways of how these functions report errors and the return values for successful parses.
Followup PRs such as rust-lang#147859 will change more and more of these "diagnostic + error"s into `LexErrors`.

The test structure with the extra module is used to allow reusing it later easily for the standalone backend.
@rust-bors

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms bot force-pushed the nonfatal-tokenstream-parse branch from 33ff2a3 to 519a3f7 Compare January 27, 2026 21:29
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms bot force-pushed the nonfatal-tokenstream-parse branch from 519a3f7 to cabd6e7 Compare February 10, 2026 10:15
@rustbot
Copy link
Collaborator

rustbot commented Feb 10, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@cyrgani cyrgani added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 10, 2026
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 11, 2026
@rust-cloud-vms rust-cloud-vms bot force-pushed the nonfatal-tokenstream-parse branch from cabd6e7 to f1c80f5 Compare February 11, 2026 20:09
@cyrgani cyrgani added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 11, 2026
@petrochenkov petrochenkov added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. S-waiting-on-t-libs-api Status: Awaiting decision from T-libs-api I-libs-api-nominated Nominated for discussion during a libs-api team meeting. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 12, 2026
@joshtriplett
Copy link
Member

We reviewed this in today's @rust-lang/libs-api meeting. This change seems reasonable. We'd like to see it run through crater, though.

Separate from that, in a different PR, we would also like to see a change to remove the case that does rustc fixups and returns a valid token stream but sets a compile error in the background, and make that produce an error all the time.

@dtolnay dtolnay linked an issue Feb 17, 2026 that may be closed by this pull request
@JonathanBrouwer
Copy link
Contributor

@bors try

rust-bors bot pushed a commit that referenced this pull request Feb 17, 2026
reduce the amount of panics in `{TokenStream, Literal}::from_str` calls
@rust-bors

This comment has been minimized.

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 17, 2026

☀️ Try build successful (CI)
Build commit: 0269aec (0269aeccce4fd29ce9ad0ebd47a62e584d7e25ef, parent: 3f6250a7bb79e600d9036bbc7c8f65af43933643)

@JonathanBrouwer
Copy link
Contributor

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-147859 created and queued.
🤖 Automatically detected try build 0269aec
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-t-libs-api Status: Awaiting decision from T-libs-api labels Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-libs-api-nominated Nominated for discussion during a libs-api team meeting. S-waiting-on-crater Status: Waiting on a crater run to be completed. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic in proc_macro::TokenStream::from_str