Skip to content

Commit 17aa775

Browse files
committed
Auto merge of #158629 - JonathanBrouwer:rollup-42QCg7x, r=JonathanBrouwer
Rollup of 7 pull requests Successful merges: - #156379 (lint on `core::ffi::c_void` as a return type) - #157347 (Implement `Box::as_non_null()`.) - #157650 (rustc_target: Add OpenEmbedded/Yocto Linux base targets) - #158569 ([rustdoc] Fix handling of inlining of `no_inline` of foreign items) - #158573 (stabilize `feature(atomic_from_mut)`) - #158614 (Fix error message when rejecting implicit stage != 2 in CI) - #158616 (Remove dependency from `rustc_metadata` on `rustc_incremental`)
2 parents f46ec52 + 16cad03 commit 17aa775

34 files changed

Lines changed: 598 additions & 118 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4392,7 +4392,6 @@ dependencies = [
43924392
"rustc_fs_util",
43934393
"rustc_hir",
43944394
"rustc_hir_pretty",
4395-
"rustc_incremental",
43964395
"rustc_index",
43974396
"rustc_macros",
43984397
"rustc_middle",

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ use rustc_errors::{
1515
};
1616
use rustc_fs_util::link_or_copy;
1717
use rustc_hir::find_attr;
18-
use rustc_incremental::{
19-
copy_cgu_workproduct_to_incr_comp_cache_dir, in_incr_comp_dir, in_incr_comp_dir_sess,
20-
};
18+
use rustc_incremental::{copy_cgu_workproduct_to_incr_comp_cache_dir, in_incr_comp_dir_sess};
2119
use rustc_macros::{Decodable, Encodable};
2220
use rustc_metadata::fs::copy_to_stdout;
2321
use rustc_middle::bug;
@@ -884,20 +882,24 @@ fn execute_copy_from_cache_work_item(
884882
let mut links_from_incr_cache = Vec::new();
885883

886884
let mut load_from_incr_comp_dir = |output_path: PathBuf, saved_path: &str| {
887-
let source_file = in_incr_comp_dir(incr_comp_session_dir, saved_path);
885+
let source_file_in_incr_comp_dir = incr_comp_session_dir.join(saved_path);
888886
debug!(
889887
"copying preexisting module `{}` from {:?} to {}",
890888
module.name,
891-
source_file,
889+
source_file_in_incr_comp_dir,
892890
output_path.display()
893891
);
894-
match link_or_copy(&source_file, &output_path) {
892+
match link_or_copy(&source_file_in_incr_comp_dir, &output_path) {
895893
Ok(_) => {
896-
links_from_incr_cache.push(source_file);
894+
links_from_incr_cache.push(source_file_in_incr_comp_dir);
897895
Some(output_path)
898896
}
899897
Err(error) => {
900-
dcx.emit_err(errors::CopyPathBuf { source_file, output_path, error });
898+
dcx.emit_err(errors::CopyPathBuf {
899+
source_file: source_file_in_incr_comp_dir,
900+
output_path,
901+
error,
902+
});
901903
None
902904
}
903905
}

compiler/rustc_incremental/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ mod diagnostics;
1010
mod persist;
1111

1212
pub use persist::{
13-
copy_cgu_workproduct_to_incr_comp_cache_dir, finalize_session_directory, in_incr_comp_dir,
14-
in_incr_comp_dir_sess, load_query_result_cache, save_work_product_index, setup_dep_graph,
13+
copy_cgu_workproduct_to_incr_comp_cache_dir, finalize_session_directory, in_incr_comp_dir_sess,
14+
load_query_result_cache, save_work_product_index, setup_dep_graph,
1515
};
1616
use rustc_middle::util::Providers;
1717

compiler/rustc_incremental/src/persist/fs.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,7 @@ fn lock_file_path(session_dir: &Path) -> PathBuf {
184184
/// Returns the path for a given filename within the incremental compilation directory
185185
/// in the current session.
186186
pub fn in_incr_comp_dir_sess(sess: &Session, file_name: &str) -> PathBuf {
187-
in_incr_comp_dir(&sess.incr_comp_session_dir(), file_name)
188-
}
189-
190-
/// Returns the path for a given filename within the incremental compilation directory,
191-
/// not necessarily from the current session.
192-
///
193-
/// To ensure the file is part of the current session, use [`in_incr_comp_dir_sess`].
194-
pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBuf {
195-
incr_comp_session_dir.join(file_name)
187+
sess.incr_comp_session_dir().join(file_name)
196188
}
197189

198190
/// Allocates the private session directory.

compiler/rustc_incremental/src/persist/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod load;
1010
mod save;
1111
mod work_product;
1212

13-
pub use fs::{finalize_session_directory, in_incr_comp_dir, in_incr_comp_dir_sess};
13+
pub use fs::{finalize_session_directory, in_incr_comp_dir_sess};
1414
pub use load::{load_query_result_cache, setup_dep_graph};
1515
pub(crate) use save::save_dep_graph;
1616
pub use save::save_work_product_index;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use rustc_abi::ExternAbi;
2+
use rustc_hir::def::Res;
3+
use rustc_hir::def_id::LocalDefId;
4+
use rustc_hir::intravisit::FnKind;
5+
use rustc_hir::{self as hir, LangItem};
6+
use rustc_session::{declare_lint, declare_lint_pass};
7+
use rustc_span::Span;
8+
9+
use crate::lints::{CVoidReturn, ExternCVoidReturn};
10+
use crate::{LateContext, LateLintPass, LintContext};
11+
12+
declare_lint! {
13+
/// The `c_void_returns` lint detects the use of [`core::ffi::c_void`] as a return type.
14+
///
15+
/// ### Example
16+
///
17+
/// ```rust
18+
/// use std::ffi::c_void;
19+
///
20+
/// unsafe extern "C" {
21+
/// fn foo() -> c_void;
22+
/// }
23+
/// ```
24+
///
25+
/// {{produces}}
26+
///
27+
/// ### Explanation
28+
///
29+
/// `c_void` is designed for use through a [`pointer`], equivalent to C's `void*` type. It is a
30+
/// mistake to use it directly as a return type, and calling `extern` functions declared as such
31+
/// may result in undefined behavior. C functions that return `void` must be declared to return
32+
/// [`()`] in Rust (omitting the return type implicitly returns `()`).
33+
///
34+
/// [`core::ffi::c_void`]: https://doc.rust-lang.org/core/ffi/enum.c_void.html
35+
/// [`pointer`]: https://doc.rust-lang.org/core/primitive.pointer.html
36+
/// [`()`]: https://doc.rust-lang.org/core/primitive.unit.html
37+
pub C_VOID_RETURNS,
38+
Warn,
39+
"detects use of `c_void` as a return type"
40+
}
41+
42+
declare_lint_pass!(CVoidReturns => [C_VOID_RETURNS]);
43+
44+
impl<'tcx> LateLintPass<'tcx> for CVoidReturns {
45+
fn check_fn(
46+
&mut self,
47+
cx: &LateContext<'tcx>,
48+
fn_kind: FnKind<'tcx>,
49+
decl: &'tcx hir::FnDecl<'tcx>,
50+
_: &'tcx hir::Body<'tcx>,
51+
_: Span,
52+
_: LocalDefId,
53+
) {
54+
check_decl(
55+
cx,
56+
decl,
57+
!matches!(fn_kind, FnKind::ItemFn(.., hir::FnHeader { abi: ExternAbi::Rust, .. })),
58+
);
59+
}
60+
61+
fn check_foreign_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ForeignItem<'tcx>) {
62+
if let hir::ForeignItemKind::Fn(sig, ..) = item.kind {
63+
check_decl(cx, sig.decl, true);
64+
}
65+
}
66+
67+
fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx hir::Ty<'tcx, hir::AmbigArg>) {
68+
if let hir::TyKind::FnPtr(fn_ptr_ty) = ty.kind {
69+
check_decl(cx, fn_ptr_ty.decl, fn_ptr_ty.abi != ExternAbi::Rust);
70+
}
71+
}
72+
}
73+
74+
fn check_decl(cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, is_extern: bool) {
75+
if let hir::FnRetTy::Return(output_ty) = decl.output
76+
&& let hir::TyKind::Path(qpath) = output_ty.kind
77+
&& let Res::Def(.., def_id) = cx.qpath_res(&qpath, output_ty.hir_id)
78+
&& cx.tcx.is_lang_item(def_id, LangItem::CVoid)
79+
{
80+
let suggestion =
81+
cx.sess().source_map().span_extend_to_prev_char(decl.output.span(), ')', true);
82+
83+
if is_extern {
84+
cx.emit_span_lint(C_VOID_RETURNS, decl.output.span(), ExternCVoidReturn { suggestion });
85+
} else {
86+
cx.emit_span_lint(C_VOID_RETURNS, decl.output.span(), CVoidReturn { suggestion });
87+
}
88+
}
89+
}

compiler/rustc_lint/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ mod async_closures;
3232
mod async_fn_in_trait;
3333
mod autorefs;
3434
pub mod builtin;
35+
mod c_void_returns;
3536
mod context;
3637
mod dangling;
3738
mod default_could_be_derived;
@@ -86,6 +87,7 @@ use async_closures::AsyncClosureUsage;
8687
use async_fn_in_trait::AsyncFnInTrait;
8788
use autorefs::*;
8889
use builtin::*;
90+
use c_void_returns::*;
8991
use dangling::*;
9092
use default_could_be_derived::DefaultCouldBeDerived;
9193
use deref_into_dyn_supertrait::*;
@@ -269,6 +271,7 @@ late_lint_methods!(
269271
LifetimeSyntax: LifetimeSyntax,
270272
InternalEqTraitMethodImpls: InternalEqTraitMethodImpls,
271273
ImplicitProvenanceCasts: ImplicitProvenanceCasts,
274+
CVoidReturns: CVoidReturns,
272275
]
273276
]
274277
);

compiler/rustc_lint/src/lints.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,33 @@ pub(crate) enum BuiltinSpecialModuleNameUsed {
614614
Main,
615615
}
616616

617+
// c_void_return.rs
618+
#[derive(Diagnostic)]
619+
#[diag("`c_void` should not be used as a return type")]
620+
#[help("returning `()` in Rust is equivalent to returning `void` in C")]
621+
pub(crate) struct CVoidReturn {
622+
#[suggestion(
623+
"remove the return type to implicitly return `()`",
624+
code = "",
625+
applicability = "maybe-incorrect"
626+
)]
627+
pub suggestion: Span,
628+
}
629+
630+
// c_void_return.rs
631+
#[derive(Diagnostic)]
632+
#[diag("declarations returning `c_void` are not compatible with C functions returning `void`")]
633+
#[help("returning `()` in Rust is equivalent to returning `void` in C")]
634+
#[note("`c_void` is only used through raw pointers for compatibility with `void` pointers")]
635+
pub(crate) struct ExternCVoidReturn {
636+
#[suggestion(
637+
"remove the return type to implicitly return `()`",
638+
code = "",
639+
applicability = "maybe-incorrect"
640+
)]
641+
pub suggestion: Span,
642+
}
643+
617644
// deref_into_dyn_supertrait.rs
618645
#[derive(Diagnostic)]
619646
#[diag("this `Deref` implementation is covered by an implicit supertrait coercion")]

compiler/rustc_metadata/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ rustc_feature = { path = "../rustc_feature" }
1818
rustc_fs_util = { path = "../rustc_fs_util" }
1919
rustc_hir = { path = "../rustc_hir" }
2020
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
21-
rustc_incremental = { path = "../rustc_incremental" }
2221
rustc_index = { path = "../rustc_index" }
2322
rustc_macros = { path = "../rustc_macros" }
2423
rustc_middle = { path = "../rustc_middle" }

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,10 +2472,10 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
24722472
&& tcx.dep_graph.try_mark_green(tcx, &dep_node).is_some()
24732473
{
24742474
let saved_path = &work_product.saved_files["rmeta"];
2475-
let incr_comp_session_dir = tcx.sess.incr_comp_session_dir_opt().unwrap();
2476-
let source_file = rustc_incremental::in_incr_comp_dir(&incr_comp_session_dir, saved_path);
2477-
debug!("copying preexisting metadata from {source_file:?} to {path:?}");
2478-
match rustc_fs_util::link_or_copy(&source_file, path) {
2475+
let incr_comp_session_dir = tcx.sess.incr_comp_session_dir();
2476+
let source_file_in_incr_dir = &incr_comp_session_dir.join(saved_path);
2477+
debug!("copying preexisting metadata from {source_file_in_incr_dir:?} to {path:?}");
2478+
match rustc_fs_util::link_or_copy(&source_file_in_incr_dir, path) {
24792479
Ok(_) => {}
24802480
Err(err) => tcx.dcx().emit_fatal(FailCreateFileEncoder { err }),
24812481
};

0 commit comments

Comments
 (0)