diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 429e62c4a1c63..9e193402feb20 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1619,13 +1619,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { debug!(?hir_bounds); let lifetime_mapping = if in_trait { - self.arena.alloc_from_iter( - collected_lifetime_mapping - .iter() - .map(|(lifetime, def_id)| (**lifetime, *def_id)), + Some( + &*self.arena.alloc_from_iter( + collected_lifetime_mapping + .iter() + .map(|(lifetime, def_id)| (**lifetime, *def_id)), + ), ) } else { - &mut [] + None }; let opaque_ty_item = hir::OpaqueTy { @@ -2090,13 +2092,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { debug!("lower_async_fn_ret_ty: generic_params={:#?}", generic_params); let lifetime_mapping = if in_trait { - self.arena.alloc_from_iter( - collected_lifetime_mapping - .iter() - .map(|(lifetime, def_id)| (**lifetime, *def_id)), + Some( + &*self.arena.alloc_from_iter( + collected_lifetime_mapping + .iter() + .map(|(lifetime, def_id)| (**lifetime, *def_id)), + ), ) } else { - &mut [] + None }; let opaque_ty_item = hir::OpaqueTy { diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index 39275272e4226..4c69b9503a2cf 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -335,6 +335,10 @@ pub fn from_fn_attrs<'ll, 'tcx>( to_add.extend(probestack_attr(cx)); to_add.extend(stackprotector_attr(cx)); + if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_BUILTINS) { + to_add.push(llvm::CreateAttrString(cx.llcx, "no-builtins")); + } + if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) { to_add.push(AttributeKind::Cold.create_attr(cx.llcx)); } diff --git a/compiler/rustc_codegen_ssa/src/codegen_attrs.rs b/compiler/rustc_codegen_ssa/src/codegen_attrs.rs index d6c2301276262..0c7b8a7961281 100644 --- a/compiler/rustc_codegen_ssa/src/codegen_attrs.rs +++ b/compiler/rustc_codegen_ssa/src/codegen_attrs.rs @@ -1,4 +1,4 @@ -use rustc_ast::{ast, MetaItemKind, NestedMetaItem}; +use rustc_ast::{ast, attr, MetaItemKind, NestedMetaItem}; use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr}; use rustc_errors::struct_span_err; use rustc_hir as hir; @@ -60,6 +60,14 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs { codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER; } + // When `no_builtins` is applied at the crate level, we should add the + // `no-builtins` attribute to each function to ensure it takes effect in LTO. + let crate_attrs = tcx.hir().attrs(rustc_hir::CRATE_HIR_ID); + let no_builtins = attr::contains_name(crate_attrs, sym::no_builtins); + if no_builtins { + codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_BUILTINS; + } + let supported_target_features = tcx.supported_target_features(LOCAL_CRATE); let mut inline_span = None; diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 35ef30114b712..68f1559ea220e 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -2664,10 +2664,19 @@ pub struct OpaqueTy<'hir> { pub generics: &'hir Generics<'hir>, pub bounds: GenericBounds<'hir>, pub origin: OpaqueTyOrigin, - // Opaques have duplicated lifetimes, this mapping connects the original lifetime with the copy - // so we can later generate bidirectional outlives predicates to enforce that these lifetimes - // stay in sync. - pub lifetime_mapping: &'hir [(Lifetime, LocalDefId)], + /// Return-position impl traits (and async futures) must "reify" any late-bound + /// lifetimes that are captured from the function signature they originate from. + /// + /// This is done by generating a new early-bound lifetime parameter local to the + /// opaque which is substituted in the function signature with the late-bound + /// lifetime. + /// + /// This mapping associated a captured lifetime (first parameter) with the new + /// early-bound lifetime that was generated for the opaque. + pub lifetime_mapping: Option<&'hir [(Lifetime, LocalDefId)]>, + /// Whether the opaque is a return-position impl trait (or async future) + /// originating from a trait method. This makes it so that the opaque is + /// lowered as an associated type. pub in_trait: bool, } diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index 1c9070600dbe3..979b101e7fe2d 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -66,7 +66,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen let opaque_ty_id = tcx.hir().local_def_id_to_hir_id(opaque_def_id.expect_local()); let opaque_ty_node = tcx.hir().get(opaque_ty_id); let Node::Item(&Item { - kind: ItemKind::OpaqueTy(OpaqueTy { lifetime_mapping, .. }), + kind: ItemKind::OpaqueTy(OpaqueTy { lifetime_mapping: Some(lifetime_mapping), .. }), .. }) = opaque_ty_node else { diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index 6522e449386c7..7c701fd4fe16e 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -956,11 +956,11 @@ pub trait LintContext: Sized { db.span_note(glob_reexport_span, format!("the name `{}` in the {} namespace is supposed to be publicly re-exported here", name, namespace)); db.span_note(private_item_span, "but the private item here shadows it".to_owned()); } - BuiltinLintDiagnostics::UnusedQualifications { path_span, unqualified_path } => { + BuiltinLintDiagnostics::UnusedQualifications { removal_span } => { db.span_suggestion_verbose( - path_span, - "replace it with the unqualified path", - unqualified_path, + removal_span, + "remove the unnecessary path segments", + "", Applicability::MachineApplicable ); } diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index f6ffd46b1fe08..10ebe29dfce69 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -551,10 +551,8 @@ pub enum BuiltinLintDiagnostics { private_item_span: Span, }, UnusedQualifications { - /// The span of the unnecessarily-qualified path. - path_span: Span, - /// The replacement unqualified path. - unqualified_path: Ident, + /// The span of the unnecessarily-qualified path to remove. + removal_span: Span, }, } diff --git a/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs b/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs index c4601a1fb4189..02fd6ed7ba6ba 100644 --- a/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs +++ b/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs @@ -100,6 +100,8 @@ bitflags! { const REALLOCATOR = 1 << 18; /// `#[rustc_allocator_zeroed]`: a hint to LLVM that the function only allocates zeroed memory. const ALLOCATOR_ZEROED = 1 << 19; + /// `#[no_builtins]`: indicates that disable implicit builtin knowledge of functions for the function. + const NO_BUILTINS = 1 << 20; } } diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 846a1ffe09b17..05128a51016a0 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -3911,8 +3911,9 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { && path[0].ident.name != kw::PathRoot && path[0].ident.name != kw::DollarCrate { + let last_segment = *path.last().unwrap(); let unqualified_result = { - match self.resolve_path(&[*path.last().unwrap()], Some(ns), None) { + match self.resolve_path(&[last_segment], Some(ns), None) { PathResult::NonModule(path_res) => path_res.expect_full_res(), PathResult::Module(ModuleOrUniformRoot::Module(module)) => { module.res().unwrap() @@ -3928,8 +3929,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { finalize.path_span, "unnecessary qualification", lint::BuiltinLintDiagnostics::UnusedQualifications { - path_span: finalize.path_span, - unqualified_path: path.last().unwrap().ident + removal_span: finalize.path_span.until(last_segment.ident.span), } ) } diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index b91e1b09330e1..7eeb987063fcb 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -4149,8 +4149,8 @@ impl<'test> TestCx<'test> { # Match paths that don't include spaces. (?:\\[\pL\pN\.\-_']+)+\.\pL+ | - # If the path starts with a well-known root, then allow spaces. - \$(?:DIR|SRC_DIR|TEST_BUILD_DIR|BUILD_DIR|LIB_DIR)(?:\\[\pL\pN\.\-_' ]+)+ + # If the path starts with a well-known root, then allow spaces and no file extension. + \$(?:DIR|SRC_DIR|TEST_BUILD_DIR|BUILD_DIR|LIB_DIR)(?:\\[\pL\pN\.\-_'\ ]+)+ )"#, ) .unwrap() diff --git a/src/tools/compiletest/src/runtest/tests.rs b/src/tools/compiletest/src/runtest/tests.rs index 51105111175a9..fb3dd326a4c80 100644 --- a/src/tools/compiletest/src/runtest/tests.rs +++ b/src/tools/compiletest/src/runtest/tests.rs @@ -8,8 +8,8 @@ fn normalize_platform_differences() { "$BUILD_DIR/../parser.rs" ); assert_eq!( - TestCx::normalize_platform_differences(r"$DIR\bar.rs hello\nworld"), - r"$DIR/bar.rs hello\nworld" + TestCx::normalize_platform_differences(r"$DIR\bar.rs: hello\nworld"), + r"$DIR/bar.rs: hello\nworld" ); assert_eq!( TestCx::normalize_platform_differences(r"either bar\baz.rs or bar\baz\mod.rs"), @@ -27,8 +27,8 @@ fn normalize_platform_differences() { ); assert_eq!(TestCx::normalize_platform_differences(r"$DIR\foo.rs:12:11"), "$DIR/foo.rs:12:11",); assert_eq!( - TestCx::normalize_platform_differences(r"$DIR\path with spaces 'n' quotes"), - "$DIR/path with spaces 'n' quotes", + TestCx::normalize_platform_differences(r"$DIR\path with\spaces 'n' quotes"), + "$DIR/path with/spaces 'n' quotes", ); assert_eq!( TestCx::normalize_platform_differences(r"$DIR\file_with\no_extension"), diff --git a/tests/codegen/no_builtins-at-crate.rs b/tests/codegen/no_builtins-at-crate.rs new file mode 100644 index 0000000000000..bb64159649144 --- /dev/null +++ b/tests/codegen/no_builtins-at-crate.rs @@ -0,0 +1,23 @@ +// compile-flags: -C opt-level=1 + +#![no_builtins] +#![crate_type = "lib"] + +// CHECK: define void @__aeabi_memcpy +// CHECK-SAME: #0 +#[no_mangle] +pub unsafe extern "C" fn __aeabi_memcpy(dest: *mut u8, src: *const u8, size: usize) { + // CHECK: call + // CHECK-SAME: @memcpy( + memcpy(dest, src, size); +} + +// CHECK: declare +// CHECK-SAME: @memcpy +// CHECK-SAME: #0 +extern "C" { + pub fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8; +} + +// CHECK: attributes #0 +// CHECK-SAME: "no-builtins" diff --git a/tests/run-make/no-builtins-attribute/Makefile b/tests/run-make/no-builtins-attribute/Makefile new file mode 100644 index 0000000000000..0ce95facaea48 --- /dev/null +++ b/tests/run-make/no-builtins-attribute/Makefile @@ -0,0 +1,9 @@ +include ../tools.mk + +# We want to check if `no-builtins` is also added to the function declarations in the used crate. + +all: + $(RUSTC) no_builtins.rs --emit=link + $(RUSTC) main.rs --emit=llvm-ir + + cat "$(TMPDIR)"/main.ll | "$(LLVM_FILECHECK)" filecheck.main.txt diff --git a/tests/run-make/no-builtins-attribute/filecheck.main.txt b/tests/run-make/no-builtins-attribute/filecheck.main.txt new file mode 100644 index 0000000000000..ecd650bdca804 --- /dev/null +++ b/tests/run-make/no-builtins-attribute/filecheck.main.txt @@ -0,0 +1,5 @@ +CHECK: declare void @foo() +CHECK-SAME: #[[ATTR_3:[0-9]+]] + +CHECK: attributes #[[ATTR_3]] +CHECK-SAME: no-builtins diff --git a/tests/run-make/no-builtins-attribute/main.rs b/tests/run-make/no-builtins-attribute/main.rs new file mode 100644 index 0000000000000..77754b37e3184 --- /dev/null +++ b/tests/run-make/no-builtins-attribute/main.rs @@ -0,0 +1,10 @@ +extern crate no_builtins; + +#[no_mangle] +fn call_foo() { + no_builtins::foo(); +} + +fn main() { + call_foo(); +} diff --git a/tests/run-make/no-builtins-attribute/no_builtins.rs b/tests/run-make/no-builtins-attribute/no_builtins.rs new file mode 100644 index 0000000000000..8ca862d2f7773 --- /dev/null +++ b/tests/run-make/no-builtins-attribute/no_builtins.rs @@ -0,0 +1,5 @@ +#![crate_type = "lib"] +#![no_builtins] + +#[no_mangle] +pub fn foo() {} diff --git a/tests/ui/lint/lint-qualification.fixed b/tests/ui/lint/lint-qualification.fixed new file mode 100644 index 0000000000000..c144930136234 --- /dev/null +++ b/tests/ui/lint/lint-qualification.fixed @@ -0,0 +1,21 @@ +// run-rustfix +#![deny(unused_qualifications)] +#![allow(deprecated)] + +mod foo { + pub fn bar() {} +} + +fn main() { + use foo::bar; + bar(); //~ ERROR: unnecessary qualification + bar(); + + let _ = || -> Result<(), ()> { try!(Ok(())); Ok(()) }; // issue #37345 + + macro_rules! m { () => { + $crate::foo::bar(); // issue #37357 + ::foo::bar(); // issue #38682 + } } + m!(); +} diff --git a/tests/ui/lint/lint-qualification.rs b/tests/ui/lint/lint-qualification.rs index 0cace0ca0355a..80904303559d8 100644 --- a/tests/ui/lint/lint-qualification.rs +++ b/tests/ui/lint/lint-qualification.rs @@ -1,3 +1,4 @@ +// run-rustfix #![deny(unused_qualifications)] #![allow(deprecated)] diff --git a/tests/ui/lint/lint-qualification.stderr b/tests/ui/lint/lint-qualification.stderr index d09cb78c4f013..90a06bc6cbe5d 100644 --- a/tests/ui/lint/lint-qualification.stderr +++ b/tests/ui/lint/lint-qualification.stderr @@ -1,18 +1,19 @@ error: unnecessary qualification - --> $DIR/lint-qualification.rs:10:5 + --> $DIR/lint-qualification.rs:11:5 | LL | foo::bar(); | ^^^^^^^^ | note: the lint level is defined here - --> $DIR/lint-qualification.rs:1:9 + --> $DIR/lint-qualification.rs:2:9 | LL | #![deny(unused_qualifications)] | ^^^^^^^^^^^^^^^^^^^^^ -help: replace it with the unqualified path +help: remove the unnecessary path segments + | +LL - foo::bar(); +LL + bar(); | -LL | bar(); - | ~~~ error: aborting due to previous error diff --git a/tests/ui/resolve/issue-113808-invalid-unused-qualifications-suggestion.fixed b/tests/ui/resolve/issue-113808-invalid-unused-qualifications-suggestion.fixed new file mode 100644 index 0000000000000..e730f94660bbd --- /dev/null +++ b/tests/ui/resolve/issue-113808-invalid-unused-qualifications-suggestion.fixed @@ -0,0 +1,31 @@ +// run-rustfix + +#![deny(unused_qualifications)] +#![feature(unsized_fn_params)] + +#[allow(unused_imports)] +use std::ops; +use std::ops::Index; + +pub struct A; + +impl Index for A { + //~^ ERROR unnecessary qualification + type Output = (); + fn index(&self, _: str) -> &Self::Output { + &() + } +} + +mod inner { + pub trait Trait {} +} + +// the import needs to be here for the lint to show up +#[allow(unused_imports)] +use inner::Trait; + +impl Trait for () {} +//~^ ERROR unnecessary qualification + +fn main() {} diff --git a/tests/ui/resolve/issue-113808-invalid-unused-qualifications-suggestion.rs b/tests/ui/resolve/issue-113808-invalid-unused-qualifications-suggestion.rs new file mode 100644 index 0000000000000..641c892e3de9a --- /dev/null +++ b/tests/ui/resolve/issue-113808-invalid-unused-qualifications-suggestion.rs @@ -0,0 +1,31 @@ +// run-rustfix + +#![deny(unused_qualifications)] +#![feature(unsized_fn_params)] + +#[allow(unused_imports)] +use std::ops; +use std::ops::Index; + +pub struct A; + +impl ops::Index for A { + //~^ ERROR unnecessary qualification + type Output = (); + fn index(&self, _: str) -> &Self::Output { + &() + } +} + +mod inner { + pub trait Trait {} +} + +// the import needs to be here for the lint to show up +#[allow(unused_imports)] +use inner::Trait; + +impl inner::Trait for () {} +//~^ ERROR unnecessary qualification + +fn main() {} diff --git a/tests/ui/resolve/issue-113808-invalid-unused-qualifications-suggestion.stderr b/tests/ui/resolve/issue-113808-invalid-unused-qualifications-suggestion.stderr new file mode 100644 index 0000000000000..d9c7fd21871ba --- /dev/null +++ b/tests/ui/resolve/issue-113808-invalid-unused-qualifications-suggestion.stderr @@ -0,0 +1,31 @@ +error: unnecessary qualification + --> $DIR/issue-113808-invalid-unused-qualifications-suggestion.rs:12:6 + | +LL | impl ops::Index for A { + | ^^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/issue-113808-invalid-unused-qualifications-suggestion.rs:3:9 + | +LL | #![deny(unused_qualifications)] + | ^^^^^^^^^^^^^^^^^^^^^ +help: remove the unnecessary path segments + | +LL - impl ops::Index for A { +LL + impl Index for A { + | + +error: unnecessary qualification + --> $DIR/issue-113808-invalid-unused-qualifications-suggestion.rs:28:6 + | +LL | impl inner::Trait for () {} + | ^^^^^^^^^^^^^^^^ + | +help: remove the unnecessary path segments + | +LL - impl inner::Trait for () {} +LL + impl Trait for () {} + | + +error: aborting due to 2 previous errors + diff --git a/tests/ui/resolve/unused-qualifications-suggestion.stderr b/tests/ui/resolve/unused-qualifications-suggestion.stderr index c8e91e07295b6..e3dac37fc6e22 100644 --- a/tests/ui/resolve/unused-qualifications-suggestion.stderr +++ b/tests/ui/resolve/unused-qualifications-suggestion.stderr @@ -9,10 +9,11 @@ note: the lint level is defined here | LL | #![deny(unused_qualifications)] | ^^^^^^^^^^^^^^^^^^^^^ -help: replace it with the unqualified path +help: remove the unnecessary path segments + | +LL - foo::bar(); +LL + bar(); | -LL | bar(); - | ~~~ error: unnecessary qualification --> $DIR/unused-qualifications-suggestion.rs:21:5 @@ -20,10 +21,11 @@ error: unnecessary qualification LL | baz::qux::quux(); | ^^^^^^^^^^^^^^ | -help: replace it with the unqualified path +help: remove the unnecessary path segments + | +LL - baz::qux::quux(); +LL + quux(); | -LL | quux(); - | ~~~~ error: aborting due to 2 previous errors