You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is an issue that was reported initially to rustfmt in rust-lang/rustfmt#3194.
The problematic code was based on the Generics.span field, which gave a wrong BytePos in case of a struct with a where clause but no parameter.
Problem
Below is an explanation of the problem, and there is a runnable example below to test it out.
Observed
With the code mod m { struct S where A: B; }, the field generics.span of the struct item is equal to Span { lo: BytePos(0), hi: BytePos(0), .. }; this points to the start of the input.
Expected
The span value should point to the position just after the struct ident, i.e., Span { lo: BytePos(16), hi: BytePos(16), .. }.
externcrate syntax;use syntax::ast::*;use syntax::ext::quote::rt::FileName;use syntax::parse;use syntax::parse::ParseSess;use syntax::source_map::FilePathMapping;fnmain(){// With source = "mod m { struct S<A> where A: B; }":// - the generics span is equal to `Span { lo: BytePos(16), hi: BytePos(19), .. }`//// With source = "mod m { struct S where A: B; }":// - the generics span is equal to `Span { lo: BytePos(0), hi: BytePos(0), .. }`// - it would be better if the span pointed to the portion just after the ident with the difference between `hi` and `lo` equal to 0, i.e., `Span { lo: BytePos(16), hi: BytePos(16), .. }`//let source = "mod m { struct S<A> where A: B; }";let source = "mod m { struct S where A: B; }";let session = ParseSess::new(FilePathMapping::empty());let file_name = FileName::Custom("source".to_string());
syntax::with_globals(|| {letmut parser = parse::new_parser_from_source_str(&session, file_name, source.to_string());let result = parser.parse_crate_mod().unwrap();let item = &result.module.items[0];ifletItemKind::Mod(module) = &item.node{let item = &module.items[0];ifletItemKind::Struct(_data, generics) = &item.node{println!("generics.span = [{:?}]", generics.span);println!("generics.params = [{:?}]", generics.params.len());}}});}
The text was updated successfully, but these errors were encountered:
@nrc Looking at the example code once again, maybe having the generics' span being (0, 0) is ok given that generics.params is empty ?
Then, the use of the generics' span in rustfmt was wrong to begin with, and there is nothing to fix for that span as written in the Expected section of the issue.
maybe having the generics' span being (0, 0) is ok given that generics.params is empty? Then, the use of the generics' span in rustfmt was wrong to begin with,
It may not be a very high priority if rustfmt has already found a better way to do what it needs to do, but I remember at least one situation where choosing a more reasonably-placed span for an "empty"/not-present AST node was useful, so I would argue that this is a legitimate bug.
Point at individual type args on arg count mismatch
- Point at individual type arguments on arg count mismatch
- Make generics always have a valid span, even when there are no args
- Explain that `impl Trait` introduces an implicit type argument
Fixrust-lang#55991.
This is an issue that was reported initially to
rustfmt
in rust-lang/rustfmt#3194.The problematic code was based on the
Generics.span
field, which gave a wrongBytePos
in case of astruct
with awhere
clause but no parameter.Problem
Below is an explanation of the problem, and there is a runnable example below to test it out.
Observed
With the code
mod m { struct S where A: B; }
, the fieldgenerics.span
of thestruct
item is equal toSpan { lo: BytePos(0), hi: BytePos(0), .. }
; this points to the start of the input.Expected
The
span
value should point to the position just after the struct ident, i.e.,Span { lo: BytePos(16), hi: BytePos(16), .. }
.Versions
Example code
rustc-ap-syntax = "297.0.0"
The text was updated successfully, but these errors were encountered: