Skip to content

Add correct span for unsized types #143580

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 1 commit into
base: master
Choose a base branch
from

Conversation

silentvein
Copy link

@silentvein silentvein commented Jul 7, 2025

Hello, here is my first PR to rust compiler, so any feedback will be appreciated

This small fix is changes span with unsized types to show only unsized type instead of showing entire type

I also get through all test updates to see if there were any regression, but everything seems good to me, I also think that no new tests needs to be added here because current tests are pretty good covers this case

fixes #141234

r? compiler

@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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Jul 7, 2025
Copy link
Author

Choose a reason for hiding this comment

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

I have some questions about this two places

if let Some(pos) = snippet.find(&self_ty) {
let start = span.lo() + BytePos(pos as u32);
let end = start + BytePos(self_ty.len() as u32);
return Some(Span::new(start, end, span.ctxt(), span.parent()));
}

if let Some(pos) = snippet.find(&core_type) {
let start = span.lo() + BytePos(pos as u32);
let end = start + BytePos(core_type.len() as u32);
return Some(Span::new(start, end, span.ctxt(), span.parent()));
}

I feel like its a code duplication but I have no idea how to improve it, so i just leaved like this, if there any better way to write it, I`d love to learn best practices

@bors
Copy link
Collaborator

bors commented Jul 7, 2025

☔ The latest upstream changes (presumably #143582) made this pull request unmergeable. Please resolve the merge conflicts.

@rust-log-analyzer

This comment has been minimized.

@lcnr
Copy link
Contributor

lcnr commented Jul 7, 2025

Hi, walking the source code to point at a more specific part of a type is unfortunately bound to be incorrect sometimes. E.g. consider

struct Foo<T: ?Sized>(*const T, str);

fn foo<T>() {}

fn main() {
    foo::<Foo<str>>()
}

I would expect your change to incorrectly point to the str arg of Foo.

We've got the same issues for well-formedness errors (caused by failing to prove a WellFormed predicate). We use a special folder here which walks over the HIR of the thing we're checking for well-formedness to get a more specific type to point at and we could in theory do a similar chedck for Sized. This would be significantly more involved however.

pub(super) fn diagnostic_hir_wf_check<'tcx>(

@silentvein
Copy link
Author

silentvein commented Jul 7, 2025

Hey, thanks a lot for your feedback! It’s really interesting for someone who doesn’t know the compiler internals well yet.

I’m still not completely sure what this means for my PR though, is there anything specific I could do here? Or, from your message, it sounds like the proper solution might be more complex than I can handle at the moment.

About your example: I tried running it, and with my changes it points to the str type, which I thought was correct, but maybe I’m misunderstanding the issue. Please feel free to correct me!

error[E0277]: the size for values of type str cannot be known at compilation time
 --> /onix/rust_projects/qwe/src/main.rs:6:15
  |
6 |     foo::<Foo<str>>();
  |               ^^^ doesn't have a size known at compile-time

There’s actually a reason why I thought this would be the right solution. I figured it might help to explain how I was thinking:
The compiler says “type str cannot be known at compile time”, so my understanding was that we should try to point exactly to the str type that the compiler mentions in the error message. That’s how I interpreted the original issue

@lcnr
Copy link
Contributor

lcnr commented Jul 8, 2025

struct Foo<T: ?Sized>(*const T, str);

fn foo<T>() {}

fn main() {
    foo::<Foo<str>>()
}

Foo<str> is unsized because it has a str field. Whether the generic parameter T is instantiated with str or u32 does not impact whether Foo is Sized. Foo<u32> is also an unsized type.

Or, from your message, it sounds like the proper solution might be more complex than I can handle at the moment.

Yeah, a 'correct' solution would have to actually walk over the HIR representation of the type (which still has span information) to point to the most appropriate part of it. Doing so is hard as you need to correctly track where we get this information from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wrong span for E0277
5 participants