From 0528693ff5dccd93c9fb8cb44c7a31b6223d3168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 7 Jan 2018 18:57:32 -0800 Subject: [PATCH] Account for `pub` in `const` -> `static` suggestion --- src/librustc_lint/builtin.rs | 7 ++++++- src/test/ui/suggestions/issue-45562.rs | 14 ++++++++++++++ src/test/ui/suggestions/issue-45562.stderr | 12 ++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/suggestions/issue-45562.rs create mode 100644 src/test/ui/suggestions/issue-45562.stderr diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index a2a6a0da68ff0..ffb844632e46f 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1214,8 +1214,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems { // don't have anything to attach a symbol to let msg = "const items should never be #[no_mangle]"; let mut err = cx.struct_span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg); + + // account for "pub const" (#45562) + let start = cx.tcx.sess.codemap().span_to_snippet(it.span) + .map(|snippet| snippet.find("const").unwrap_or(0)) + .unwrap_or(0) as u32; // `const` is 5 chars - let const_span = it.span.with_hi(BytePos(it.span.lo().0 + 5)); + let const_span = it.span.with_hi(BytePos(it.span.lo().0 + start + 5)); err.span_suggestion(const_span, "try a static value", "pub static".to_owned()); diff --git a/src/test/ui/suggestions/issue-45562.rs b/src/test/ui/suggestions/issue-45562.rs new file mode 100644 index 0000000000000..f493df56f949d --- /dev/null +++ b/src/test/ui/suggestions/issue-45562.rs @@ -0,0 +1,14 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[no_mangle] pub const RAH: usize = 5; +//~^ ERROR const items should never be #[no_mangle] + +fn main() {} diff --git a/src/test/ui/suggestions/issue-45562.stderr b/src/test/ui/suggestions/issue-45562.stderr new file mode 100644 index 0000000000000..2f8c4cd3f2e72 --- /dev/null +++ b/src/test/ui/suggestions/issue-45562.stderr @@ -0,0 +1,12 @@ +error: const items should never be #[no_mangle] + --> $DIR/issue-45562.rs:11:14 + | +11 | #[no_mangle] pub const RAH: usize = 5; + | ---------^^^^^^^^^^^^^^^^ + | | + | help: try a static value: `pub static` + | + = note: #[deny(no_mangle_const_items)] on by default + +error: aborting due to previous error +