Skip to content

Commit af60769

Browse files
authored
Rollup merge of rust-lang#103675 - lyming2007:issue-103271-fix, r=fee1-dead
remove redundent "<>" for ty::Slice with reference type this fix rust-lang#103271
2 parents 432b1a4 + 868ccd5 commit af60769

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

Diff for: compiler/rustc_hir_typeck/src/method/suggest.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18651865
| ty::Str
18661866
| ty::Projection(_)
18671867
| ty::Param(_) => format!("{deref_ty}"),
1868+
// we need to test something like <&[_]>::len
1869+
// and Vec::function();
1870+
// <&[_]>::len doesn't need an extra "<>" between
1871+
// but for Adt type like Vec::function()
1872+
// we would suggest <[_]>::function();
1873+
_ if self.tcx.sess.source_map().span_wrapped_by_angle_bracket(ty.span) => format!("{deref_ty}"),
18681874
_ => format!("<{deref_ty}>"),
18691875
};
18701876
err.span_suggestion_verbose(

Diff for: compiler/rustc_span/src/source_map.rs

+44
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,50 @@ impl SourceMap {
753753
}
754754
}
755755

756+
/// Given a 'Span', tries to tell if the next character is '>'
757+
/// and the previous charactoer is '<' after skipping white space
758+
/// return true if wrapped by '<>'
759+
pub fn span_wrapped_by_angle_bracket(&self, span: Span) -> bool {
760+
self.span_to_source(span, |src, start_index, end_index| {
761+
if src.get(start_index..end_index).is_none() {
762+
return Ok(false);
763+
}
764+
// test the right side to match '>' after skipping white space
765+
let end_src = &src[end_index..];
766+
let mut i = 0;
767+
while let Some(cc) = end_src.chars().nth(i) {
768+
if cc == ' ' {
769+
i = i + 1;
770+
} else if cc == '>' {
771+
// found > in the right;
772+
break;
773+
} else {
774+
// failed to find '>' return false immediately
775+
return Ok(false);
776+
}
777+
}
778+
// test the left side to match '<' after skipping white space
779+
i = start_index;
780+
let start_src = &src[0..start_index];
781+
while let Some(cc) = start_src.chars().nth(i) {
782+
if cc == ' ' {
783+
if i == 0 {
784+
return Ok(false);
785+
}
786+
i = i - 1;
787+
} else if cc == '<' {
788+
// found < in the left
789+
break;
790+
} else {
791+
// failed to find '<' return false immediately
792+
return Ok(false);
793+
}
794+
}
795+
return Ok(true);
796+
})
797+
.map_or(false, |is_accessible| is_accessible)
798+
}
799+
756800
/// Given a `Span`, tries to get a shorter span ending just after the first occurrence of `char`
757801
/// `c`.
758802
pub fn span_through_char(&self, sp: Span, c: char) -> Span {

Diff for: src/test/ui/type/issue-103271.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
//~^ HELP the following trait is implemented but not in scope; perhaps add a `use` for it:
3+
let length = <&[_]>::len;
4+
//~^ ERROR the function or associated item `len` exists for reference `&[_]`, but its trait bounds were not satisfied [E0599]
5+
//~| function or associated item cannot be called on `&[_]` due to unsatisfied trait bounds
6+
//~| HELP items from traits can only be used if the trait is in scope
7+
//~| HELP the function `len` is implemented on `[_]`
8+
assert_eq!(length(&[1,3]), 2);
9+
}

Diff for: src/test/ui/type/issue-103271.stderr

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0599]: the function or associated item `len` exists for reference `&[_]`, but its trait bounds were not satisfied
2+
--> $DIR/issue-103271.rs:3:26
3+
|
4+
LL | let length = <&[_]>::len;
5+
| ^^^ function or associated item cannot be called on `&[_]` due to unsatisfied trait bounds
6+
|
7+
= note: the following trait bounds were not satisfied:
8+
`&[_]: ExactSizeIterator`
9+
which is required by `&mut &[_]: ExactSizeIterator`
10+
= help: items from traits can only be used if the trait is in scope
11+
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
12+
|
13+
LL | use object::read::read_ref::ReadRef;
14+
|
15+
help: the function `len` is implemented on `[_]`
16+
|
17+
LL | let length = <[_]>::len;
18+
| ~~~
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)