Skip to content

Snippet rework #14724

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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Snippet rework #14724

wants to merge 5 commits into from

Conversation

Jarcho
Copy link
Contributor

@Jarcho Jarcho commented May 2, 2025

This is a further rework of our snippet accessing/creating code.

The general design goals with this:

  • Avoid allocating when not strictly necessary.
  • Make it easy to avoid linting if an invalid span is used.
  • Make it easier to debug when an invalid span is used.
  • Make it easy to minimize the number of lookups in the source map.
  • Make it easy to avoid creating intermediary compressed spans.
  • Make it as easy as possible to compose span adjustments/checks while maintaining the previous goals.

The main implementation is split across two types.

  • SourceText which holds the Arc<SourceFile> and a pointer to the text. This type guarantees that the source is accessible and also implements Display, Deref<Target=str> and others.
  • SourceFileRange which holds a SourceText instance over the whole file and a sub-range in that file which can be manipulated. All span editing functions are on this type as well as conversion back into a Span.

The number of functions on SpanExt (formerly SpanRangeExt) has been cut down to four. get_source_range is not actually used, but remains since it's the most general way to access everything and may end up being needed. All the span editing functions have been moved onto SourceFileRange with only a single map_span function in their place. This should make span edits a little more composable.

Without debug assertions, None will be returned when an invalid span is used to index the source text. With debug assertions all spans and span changed are asserted for validity (can be used to index the source in a single file). In both cases the source being unavailable will return None.

changelog: None

@rustbot
Copy link
Collaborator

rustbot commented May 2, 2025

r? @Alexendoo

rustbot has assigned @Alexendoo.
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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 2, 2025
@Jarcho
Copy link
Contributor Author

Jarcho commented May 2, 2025

The first two commits are mainly renames. The third commit has all the actual changes.

@Jarcho Jarcho force-pushed the source_rework branch 2 times, most recently from 91b8913 to b336277 Compare May 2, 2025 05:13
@Jarcho
Copy link
Contributor Author

Jarcho commented May 2, 2025

Looking at MISSING_DOCS_IN_PRIVATE_ITEMS, that lint needs to be restructured a bit. Macros currently mess up it's implementation quite a bit.

Why is this not part of rustc in the first place?

@rustbot
Copy link
Collaborator

rustbot commented May 2, 2025

☔ The latest upstream changes (possibly 56f0182) made this pull request unmergeable. Please resolve the merge conflicts.

Comment on lines 272 to 407
/// Handle to a source file's text and a range within that file.
///
/// With debug assertions the range is checked to be a valid substring of the source text. Without
/// assertions `None` will be returned from various functions when accessing the substring of the
/// source text fails.
#[derive(Clone)]
pub struct SourceFileRange {
file: SourceText,
range: Range<RelativeBytePos>,
}
Copy link
Member

Choose a reason for hiding this comment

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

Both SourceFileRange and SourceText can represent subsets of a source file, could they be merged into a single type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doing so would require that SourceFileRange always validate it's range. The main point of SourceText is that it's definitely a valid string.

They're also serving two different purpose. One is a substring of the source text, the other is a movable view of a whole file.

Copy link
Member

Choose a reason for hiding this comment

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

Always validating doesn't seem so bad to me, API wise it's some extra ?ing when using with_lo/with_hi

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They're still modelling two completely different things. Even if only one of them is exposed using both to implement it is still useful.

Comment on lines 287 to 301
&& let Some(span) = new_lhs.span.map_span(cx, |file| {
let src = file.with_hi(span.hi()).src_text()?;
// Do not continue if we have mismatched number of parens, otherwise the suggestion is wrong
src.matches('(').count() == src.matches(')').count()
(src.matches('(').count() == src.matches(')').count()).then_some(file)
})
Copy link
Member

Choose a reason for hiding this comment

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

with_hi mutating file here is confusing, there's similar elsewhere with trim_start/trim_end

Reusing names of non mutating methods while also returning a value makes it difficult to realise what's happening

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, those need to be renamed.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jul 18, 2025
Copy link

github-actions bot commented Jul 18, 2025

Lintcheck changes for f0d6813

Lint Added Removed Changed
clippy::missing_docs_in_private_items 150 1883 22257
clippy::too_long_first_doc_paragraph 0 2 2

This comment will be updated if you push new changes

@Jarcho Jarcho added the S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work label Jul 18, 2025
github-merge-queue bot pushed a commit that referenced this pull request Jul 19, 2025
This is blocking #14724

changelog: none
github-merge-queue bot pushed a commit that referenced this pull request Jul 19, 2025
This is blocking #14724

changelog: none
Jarcho added 3 commits July 19, 2025 11:03
* Don't search spans between items for comments.
* Don't lint items inside bodies, automatically derived impls.
* Don't lint items inside macro generated modules unless they are namable outside.
* Delay `is_from_proc_macro` checks
* Handle reexports when checking if an item is reachable from the crate root.
* Don't lint the crate root.
@rustbot rustbot removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) has-merge-commits PR has merge commits, merge with caution. labels Jul 19, 2025
@Jarcho
Copy link
Contributor Author

Jarcho commented Jul 20, 2025

Should be ready, but still waiting on #14741 to be merged. Recent changes:

  • Debug assert message now has a lot more info. The amount of info added is quite helpful when debugging span issues. SourceMap has to be threaded through when debug assertions are enabled. to make the cross file notes.
  • #[track_caller] is used to get the panic location to give a useful position. This is way easier then reading the stack trace.
  • Setting the start and end are split into if_within and if_before/after versions. The caller should always know which one should happen, but macros can cause tokens to get rearranged in fun ways.

I'm still very much against trying to merge SourceText and SourceFileRange. They model notably different things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants