diff --git a/src/librustc_hir/hir.rs b/src/librustc_hir/hir.rs index 7d1cb7738c3..f3dfec7ca72 100644 --- a/src/librustc_hir/hir.rs +++ b/src/librustc_hir/hir.rs @@ -1511,13 +1511,7 @@ pub fn is_range_literal(sm: &SourceMap, expr: &Expr<'_>) -> bool { // Check whether a span corresponding to a range expression is a // range literal, rather than an explicit struct or `new()` call. fn is_lit(sm: &SourceMap, span: &Span) -> bool { - let end_point = sm.end_point(*span); - - if let Ok(end_string) = sm.span_to_snippet(end_point) { - !(end_string.ends_with('}') || end_string.ends_with(')')) - } else { - false - } + sm.span_to_snippet(*span).map(|range_src| range_src.contains("..")).unwrap_or(false) }; match expr.kind { diff --git a/src/test/ui/range/issue-73553-misinterp-range-literal.rs b/src/test/ui/range/issue-73553-misinterp-range-literal.rs new file mode 100644 index 00000000000..e65dba0a038 --- /dev/null +++ b/src/test/ui/range/issue-73553-misinterp-range-literal.rs @@ -0,0 +1,16 @@ +type Range = std::ops::Range; + +fn demo(r: &Range) { + println!("{:?}", r); +} + +fn tell(x: usize) -> usize { + x +} + +fn main() { + demo(tell(1)..tell(10)); + //~^ ERROR mismatched types + demo(1..10); + //~^ ERROR mismatched types +} diff --git a/src/test/ui/range/issue-73553-misinterp-range-literal.stderr b/src/test/ui/range/issue-73553-misinterp-range-literal.stderr new file mode 100644 index 00000000000..5167b87fd27 --- /dev/null +++ b/src/test/ui/range/issue-73553-misinterp-range-literal.stderr @@ -0,0 +1,27 @@ +error[E0308]: mismatched types + --> $DIR/issue-73553-misinterp-range-literal.rs:12:10 + | +LL | demo(tell(1)..tell(10)); + | ^^^^^^^^^^^^^^^^^ + | | + | expected reference, found struct `std::ops::Range` + | help: consider borrowing here: `&(tell(1)..tell(10))` + | + = note: expected reference `&std::ops::Range` + found struct `std::ops::Range` + +error[E0308]: mismatched types + --> $DIR/issue-73553-misinterp-range-literal.rs:14:10 + | +LL | demo(1..10); + | ^^^^^ + | | + | expected reference, found struct `std::ops::Range` + | help: consider borrowing here: `&(1..10)` + | + = note: expected reference `&std::ops::Range` + found struct `std::ops::Range<{integer}>` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`.