For E0277 on for
loops, point at first line
When E0277's span points at a `for` loop, the actual issue is in the element being iterated. Instead of pointing at the entire loop, point only at the first line (when possible) so that the span ends in the element for which E0277 was triggered.
This commit is contained in:
parent
3a39b2aa5a
commit
3a530baec9
@ -551,6 +551,24 @@ pub fn report_selection_error(&self,
|
||||
let OnUnimplementedNote { message, label }
|
||||
= self.on_unimplemented_note(trait_ref, obligation);
|
||||
let have_alt_message = message.is_some() || label.is_some();
|
||||
let span = match self.tcx.sess.codemap().span_to_snippet(span) {
|
||||
Ok(ref s) if s.starts_with("for ") => {
|
||||
// On for loops, this error is caused by the element being iterated
|
||||
// on, but the span points at the entire for loop. Instead of:
|
||||
//
|
||||
// / for c in "asdf" {
|
||||
// | ...
|
||||
// | }
|
||||
// |_^ `&str` is not an iterator
|
||||
//
|
||||
// lets point at:
|
||||
//
|
||||
// for c in "asdf" {
|
||||
// ^^^^^^^^^^^^^^^ `&str` is not an iterator
|
||||
self.tcx.sess.codemap().span_until_char(span, '{')
|
||||
}
|
||||
_ => span,
|
||||
};
|
||||
|
||||
let mut err = struct_span_err!(
|
||||
self.tcx.sess,
|
||||
|
21
src/test/ui/suggestions/for-c-in-str.rs
Normal file
21
src/test/ui/suggestions/for-c-in-str.rs
Normal file
@ -0,0 +1,21 @@
|
||||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// E0277 should point exclusively at line 14, not the entire for loop span
|
||||
|
||||
fn main() {
|
||||
for c in "asdf" {
|
||||
//~^ ERROR the trait bound `&str: std::iter::Iterator` is not satisfied
|
||||
//~| NOTE `&str` is not an iterator
|
||||
//~| HELP the trait `std::iter::Iterator` is not implemented for `&str`
|
||||
//~| NOTE required by `std::iter::IntoIterator::into_iter`
|
||||
println!("");
|
||||
}
|
||||
}
|
11
src/test/ui/suggestions/for-c-in-str.stderr
Normal file
11
src/test/ui/suggestions/for-c-in-str.stderr
Normal file
@ -0,0 +1,11 @@
|
||||
error[E0277]: the trait bound `&str: std::iter::Iterator` is not satisfied
|
||||
--> $DIR/for-c-in-str.rs:14:5
|
||||
|
|
||||
14 | for c in "asdf" {
|
||||
| ^^^^^^^^^^^^^^^ `&str` is not an iterator; maybe try calling `.iter()` or a similar method
|
||||
|
|
||||
= help: the trait `std::iter::Iterator` is not implemented for `&str`
|
||||
= note: required by `std::iter::IntoIterator::into_iter`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
Loading…
Reference in New Issue
Block a user