Skip to content

Commit

Permalink
Rollup merge of rust-lang#103675 - lyming2007:issue-103271-fix, r=fee…
Browse files Browse the repository at this point in the history
…1-dead

remove redundent "<>" for ty::Slice with reference type

this fix rust-lang#103271
  • Loading branch information
Dylan-DPC authored Nov 3, 2022
2 parents 432b1a4 + 868ccd5 commit af60769
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
| ty::Str
| ty::Projection(_)
| ty::Param(_) => format!("{deref_ty}"),
// we need to test something like <&[_]>::len
// and Vec::function();
// <&[_]>::len doesn't need an extra "<>" between
// but for Adt type like Vec::function()
// we would suggest <[_]>::function();
_ if self.tcx.sess.source_map().span_wrapped_by_angle_bracket(ty.span) => format!("{deref_ty}"),
_ => format!("<{deref_ty}>"),
};
err.span_suggestion_verbose(
Expand Down
44 changes: 44 additions & 0 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,50 @@ impl SourceMap {
}
}

/// Given a 'Span', tries to tell if the next character is '>'
/// and the previous charactoer is '<' after skipping white space
/// return true if wrapped by '<>'
pub fn span_wrapped_by_angle_bracket(&self, span: Span) -> bool {
self.span_to_source(span, |src, start_index, end_index| {
if src.get(start_index..end_index).is_none() {
return Ok(false);
}
// test the right side to match '>' after skipping white space
let end_src = &src[end_index..];
let mut i = 0;
while let Some(cc) = end_src.chars().nth(i) {
if cc == ' ' {
i = i + 1;
} else if cc == '>' {
// found > in the right;
break;
} else {
// failed to find '>' return false immediately
return Ok(false);
}
}
// test the left side to match '<' after skipping white space
i = start_index;
let start_src = &src[0..start_index];
while let Some(cc) = start_src.chars().nth(i) {
if cc == ' ' {
if i == 0 {
return Ok(false);
}
i = i - 1;
} else if cc == '<' {
// found < in the left
break;
} else {
// failed to find '<' return false immediately
return Ok(false);
}
}
return Ok(true);
})
.map_or(false, |is_accessible| is_accessible)
}

/// Given a `Span`, tries to get a shorter span ending just after the first occurrence of `char`
/// `c`.
pub fn span_through_char(&self, sp: Span, c: char) -> Span {
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/type/issue-103271.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn main() {
//~^ HELP the following trait is implemented but not in scope; perhaps add a `use` for it:
let length = <&[_]>::len;
//~^ ERROR the function or associated item `len` exists for reference `&[_]`, but its trait bounds were not satisfied [E0599]
//~| function or associated item cannot be called on `&[_]` due to unsatisfied trait bounds
//~| HELP items from traits can only be used if the trait is in scope
//~| HELP the function `len` is implemented on `[_]`
assert_eq!(length(&[1,3]), 2);
}
22 changes: 22 additions & 0 deletions src/test/ui/type/issue-103271.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0599]: the function or associated item `len` exists for reference `&[_]`, but its trait bounds were not satisfied
--> $DIR/issue-103271.rs:3:26
|
LL | let length = <&[_]>::len;
| ^^^ function or associated item cannot be called on `&[_]` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`&[_]: ExactSizeIterator`
which is required by `&mut &[_]: ExactSizeIterator`
= help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
LL | use object::read::read_ref::ReadRef;
|
help: the function `len` is implemented on `[_]`
|
LL | let length = <[_]>::len;
| ~~~

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.

0 comments on commit af60769

Please sign in to comment.