Skip to content

Span markers in diagnostics can be confusing if text above wraps #42112

Closed
@crumblingstatue

Description

@crumblingstatue

Code:

use std::rc::Rc;
use std::cell::RefCell;

struct MyType;

fn exec_closure<F: Fn() + 'static>(f: F) {}

fn just_do_it(my_arg_1: Rc<RefCell<MyType>>, my_arg_2: &MyType, my_arg_3: Rc<RefCell<MyType>>, my_arg_4: Rc<RefCell<MyType>>){
    exec_closure(move || {
        my_arg_1;
    });
}

fn main() {}

This is how the error is displayed on a 80 column terminal

error[E0507]: cannot move out of captured outer variable in an `Fn` closure
  --> foo.rs:10:9
   |
8  | fn just_do_it(my_arg_1: Rc<RefCell<MyType>>, my_arg_2: &MyType, my_arg_3: R
c<RefCell<MyType>>, my_arg_4: Rc<RefCell<MyType>>){ % Wrapped line %
   |               -------- captured outer variable % Makes it look like it's
                                                      Pointing to my_arg_4 %
9  |     exec_closure(move || {
10 |         my_arg_1;
   |         ^^^^^^^^ cannot move out of captured outer variable in an `Fn` clos
ure

It's even worse in the real-life scenario that inspired this:

The second span marker just lazily points to the top of the closure.

Possible improvements:

  • Perhaps the irrelevant part could be omitted:

    error[E0507]: cannot move out of captured outer variable in an `Fn` closure
      --> foo.rs:10:9
       |
    8  | fn just_do_it(my_arg_1: Rc<RefCell<MyType>>, ...
       |               -------- captured outer variable 
       |
    9  |     exec_closure(move || {
    10 |         my_arg_1;
       |         ^^^^^^^^ cannot move out of captured outer variable in an `Fn` clos
    ure
    
    

    An additional advantage of this method is that there is less spam overall. The less irrelevant information there is, the more comprehensible the diagnostic becomes. It can also be applied in both directions. For example, if the relevant arg is in the middle, it could be displayed like fn just_do_it(..., relevant_arg, ...).

  • If the overflowing part should be kept for some reason, maybe it could be modified to make
    the span marker fit in somehow.

    error[E0507]: cannot move out of captured outer variable in an `Fn` closure
      --> foo.rs:10:9
       |
    8  | fn just_do_it(my_arg_1: Rc<RefCell<MyType>>, my_arg_2: &MyType, my_arg_3: R
    c<RefCell<MyType>>,-------- my_arg_4: Rc<RefCell<MyType>>){ % Wrapped line %
       |               | captured outer variable
       |
    9  |     exec_closure(move || {
    10 |         my_arg_1;
       |         ^^^^^^^^ cannot move out of captured outer variable in an `Fn` clos
    ure
    
    
  • my_arg_1 could be highlighted directly. This would require the use of color or making the font bold.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions