Skip to content

Commit 0b6934d

Browse files
author
Yiming Lei
committed
remove redundent "<>" for ty::Slice with reference type
this fix #103271
1 parent 73c9eaf commit 0b6934d

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -1804,6 +1804,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18041804
| ty::Str
18051805
| ty::Projection(_)
18061806
| ty::Param(_) => format!("{deref_ty}"),
1807+
// we need to test something like <&[_]>::len
1808+
// and Vec::function();
1809+
// <&[_]>::len doesn't need an extra "<>" between
1810+
// but for Adt type like Vec::function()
1811+
// we would suggest <[_]>::function();
1812+
_ if self.tcx.sess.source_map().span_wrapped_by_angle_bracket(ty.span) => format!("{deref_ty}"),
18071813
_ => format!("<{deref_ty}>"),
18081814
};
18091815
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

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {
2+
let iter_fun = <&[u32]>::iter;
3+
//~^ ERROR no function or associated item named `iter` found for reference `&[u32]` in the current scope [E0599]
4+
//~| function or associated item not found in `&[u32]`
5+
//~| HELP the function `iter` is implemented on `[u32]`
6+
for item in iter_fun(&[1,1]) {
7+
let x: &u32 = item;
8+
assert_eq!(x, &1);
9+
}
10+
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0599]: no function or associated item named `iter` found for reference `&[u32]` in the current scope
2+
--> $DIR/issue-103271.rs:2:30
3+
|
4+
LL | let iter_fun = <&[u32]>::iter;
5+
| ^^^^ function or associated item not found in `&[u32]`
6+
|
7+
help: the function `iter` is implemented on `[u32]`
8+
|
9+
LL | let iter_fun = <[u32]>::iter;
10+
| ~~~~~
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)