Skip to content

ranges can be funny #17

@matthiaskrgr

Description

@matthiaskrgr

The following code

//@ run-pass
//@ edition: 2021
// Test that the precedence of ranges is correct

use core::ops::{Add, RangeTo};

struct Foo {
    foo: usize,
}

impl Foo {
    fn bar(&self) -> usize {
        5
    }
}

impl Add<RangeTo<usize>> for Foo {
    type Output = usize;
    fn add(self, range: RangeTo<usize>) -> Self::Output {
        self.foo + range.end
    }
}

fn main() {
    let x = Foo { foo: 3 } + ..4..;
    assert_eq!(x, (4..));
}

Compiled successfully ( with --crate-type=lib --cap-lints=warn --edition=2021 ):

warning: method `bar` is never used
  --> a.rs:12:8
   |
11 | impl Foo {
   | -------- method in this implementation
12 |     fn bar(&self) -> usize {
   |        ^^^
   |
   = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

warning: 1 warning emitted

However rasur was found to have errors (-e 2021):

error: range operators cannot be chained
  --> ./a.rs:25:33
   |
25 |     let x = Foo { foo: 3 } + ..4..;
   |                                 ^^

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions