Skip to content

feature(new_range) applies to macro expansions and breaks their assumptions #148342

@kpreid

Description

@kpreid

#![feature(new_range)] changes the meaning of .. syntax for the entire build, regardless of where the tokens came from. Therefore, it can break macros defined by crates not using new_range. I think it should instead be activated based on the span of the .. token, similar to how edition-specific behaviors are often implemented.

I encountered this while trying to test feature(new_range) in code that uses arcstr::literal_substr!.

Repro

Substitute crate name in the doctest as needed.

//! ```
//! #![feature(new_range, new_range_api)]
//! // should print 5
//! dbg!(playground::example!("hello"));
//! ```

#[macro_export]
macro_rules! example {
    ($s:literal) => {
        $crate::takes_range(0..$s.len())
    };
}

pub fn takes_range(range: core::ops::Range<usize>) -> usize {
    range.end
}

I expected to see this happen: The use of a macro defined by another crate should successfully compile.

Instead, this happened:

error[E0308]: mismatched types
  --> src/lib.rs:6:6
   |
 8 | dbg!(scratchpad::example!("hello"));
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |      |
   |      expected `std::ops::Range<usize>`, found `std::range::Range<usize>`
   |      arguments to this function are incorrect
   |
   = note: expected struct `std::ops::Range<usize>`
              found struct `std::range::Range<usize>`
note: function defined here
  --> src/lib.rs:14:8
   |
14 | pub fn takes_range(range: core::ops::Range<usize>) -> usize {
   |        ^^^^^^^^^^^
   = note: this error originates in the macro `scratchpad::example` (in Nightly builds, run with -Z macro-backtrace for more info)
help: call `Into::into` on this expression to convert `std::range::Range<usize>` into `std::ops::Range<usize>`
   |
 8 | dbg!(scratchpad::example!("hello").into());
   |                                   +++++++

Meta

rustc --version --verbose:

rustc 1.93.0-nightly (292be5c7c 2025-10-29)
binary: rustc
commit-hash: 292be5c7c05138d753bbd4b30db7a3f1a5c914f7
commit-date: 2025-10-29
host: aarch64-apple-darwin
release: 1.93.0-nightly
LLVM version: 21.1.3

@rustbot label +F-new_range

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-new_range`#![feature(new_range)]`needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions