Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4392,7 +4392,6 @@ dependencies = [
"rustc_fs_util",
"rustc_hir",
"rustc_hir_pretty",
"rustc_incremental",
"rustc_index",
"rustc_macros",
"rustc_middle",
Expand Down
18 changes: 10 additions & 8 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ use rustc_errors::{
};
use rustc_fs_util::link_or_copy;
use rustc_hir::find_attr;
use rustc_incremental::{
copy_cgu_workproduct_to_incr_comp_cache_dir, in_incr_comp_dir, in_incr_comp_dir_sess,
};
use rustc_incremental::{copy_cgu_workproduct_to_incr_comp_cache_dir, in_incr_comp_dir_sess};
use rustc_macros::{Decodable, Encodable};
use rustc_metadata::fs::copy_to_stdout;
use rustc_middle::bug;
Expand Down Expand Up @@ -884,20 +882,24 @@ fn execute_copy_from_cache_work_item(
let mut links_from_incr_cache = Vec::new();

let mut load_from_incr_comp_dir = |output_path: PathBuf, saved_path: &str| {
let source_file = in_incr_comp_dir(incr_comp_session_dir, saved_path);
let source_file_in_incr_comp_dir = incr_comp_session_dir.join(saved_path);
debug!(
"copying preexisting module `{}` from {:?} to {}",
module.name,
source_file,
source_file_in_incr_comp_dir,
output_path.display()
);
match link_or_copy(&source_file, &output_path) {
match link_or_copy(&source_file_in_incr_comp_dir, &output_path) {
Ok(_) => {
links_from_incr_cache.push(source_file);
links_from_incr_cache.push(source_file_in_incr_comp_dir);
Some(output_path)
}
Err(error) => {
dcx.emit_err(errors::CopyPathBuf { source_file, output_path, error });
dcx.emit_err(errors::CopyPathBuf {
source_file: source_file_in_incr_comp_dir,
output_path,
error,
});
None
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_incremental/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ mod diagnostics;
mod persist;

pub use persist::{
copy_cgu_workproduct_to_incr_comp_cache_dir, finalize_session_directory, in_incr_comp_dir,
in_incr_comp_dir_sess, load_query_result_cache, save_work_product_index, setup_dep_graph,
copy_cgu_workproduct_to_incr_comp_cache_dir, finalize_session_directory, in_incr_comp_dir_sess,
load_query_result_cache, save_work_product_index, setup_dep_graph,
};
use rustc_middle::util::Providers;

Expand Down
10 changes: 1 addition & 9 deletions compiler/rustc_incremental/src/persist/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,7 @@ fn lock_file_path(session_dir: &Path) -> PathBuf {
/// Returns the path for a given filename within the incremental compilation directory
/// in the current session.
pub fn in_incr_comp_dir_sess(sess: &Session, file_name: &str) -> PathBuf {
in_incr_comp_dir(&sess.incr_comp_session_dir(), file_name)
}

/// Returns the path for a given filename within the incremental compilation directory,
/// not necessarily from the current session.
///
/// To ensure the file is part of the current session, use [`in_incr_comp_dir_sess`].
pub fn in_incr_comp_dir(incr_comp_session_dir: &Path, file_name: &str) -> PathBuf {
incr_comp_session_dir.join(file_name)
sess.incr_comp_session_dir().join(file_name)
}

/// Allocates the private session directory.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/persist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod load;
mod save;
mod work_product;

pub use fs::{finalize_session_directory, in_incr_comp_dir, in_incr_comp_dir_sess};
pub use fs::{finalize_session_directory, in_incr_comp_dir_sess};
pub use load::{load_query_result_cache, setup_dep_graph};
pub(crate) use save::save_dep_graph;
pub use save::save_work_product_index;
Expand Down
89 changes: 89 additions & 0 deletions compiler/rustc_lint/src/c_void_returns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use rustc_abi::ExternAbi;
use rustc_hir::def::Res;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::FnKind;
use rustc_hir::{self as hir, LangItem};
use rustc_session::{declare_lint, declare_lint_pass};
use rustc_span::Span;

use crate::lints::{CVoidReturn, ExternCVoidReturn};
use crate::{LateContext, LateLintPass, LintContext};

declare_lint! {
/// The `c_void_returns` lint detects the use of [`core::ffi::c_void`] as a return type.
///
/// ### Example
///
/// ```rust
/// use std::ffi::c_void;
///
/// unsafe extern "C" {
/// fn foo() -> c_void;
/// }
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// `c_void` is designed for use through a [`pointer`], equivalent to C's `void*` type. It is a
/// mistake to use it directly as a return type, and calling `extern` functions declared as such
/// may result in undefined behavior. C functions that return `void` must be declared to return
/// [`()`] in Rust (omitting the return type implicitly returns `()`).
///
/// [`core::ffi::c_void`]: https://doc.rust-lang.org/core/ffi/enum.c_void.html
/// [`pointer`]: https://doc.rust-lang.org/core/primitive.pointer.html
/// [`()`]: https://doc.rust-lang.org/core/primitive.unit.html
pub C_VOID_RETURNS,
Warn,
"detects use of `c_void` as a return type"
}

declare_lint_pass!(CVoidReturns => [C_VOID_RETURNS]);

impl<'tcx> LateLintPass<'tcx> for CVoidReturns {
fn check_fn(
&mut self,
cx: &LateContext<'tcx>,
fn_kind: FnKind<'tcx>,
decl: &'tcx hir::FnDecl<'tcx>,
_: &'tcx hir::Body<'tcx>,
_: Span,
_: LocalDefId,
) {
check_decl(
cx,
decl,
!matches!(fn_kind, FnKind::ItemFn(.., hir::FnHeader { abi: ExternAbi::Rust, .. })),
);
}

fn check_foreign_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ForeignItem<'tcx>) {
if let hir::ForeignItemKind::Fn(sig, ..) = item.kind {
check_decl(cx, sig.decl, true);
}
}

fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx hir::Ty<'tcx, hir::AmbigArg>) {
if let hir::TyKind::FnPtr(fn_ptr_ty) = ty.kind {
check_decl(cx, fn_ptr_ty.decl, fn_ptr_ty.abi != ExternAbi::Rust);
}
}
}

fn check_decl(cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, is_extern: bool) {
if let hir::FnRetTy::Return(output_ty) = decl.output
&& let hir::TyKind::Path(qpath) = output_ty.kind
&& let Res::Def(.., def_id) = cx.qpath_res(&qpath, output_ty.hir_id)
&& cx.tcx.is_lang_item(def_id, LangItem::CVoid)
{
let suggestion =
cx.sess().source_map().span_extend_to_prev_char(decl.output.span(), ')', true);

if is_extern {
cx.emit_span_lint(C_VOID_RETURNS, decl.output.span(), ExternCVoidReturn { suggestion });
} else {
cx.emit_span_lint(C_VOID_RETURNS, decl.output.span(), CVoidReturn { suggestion });
}
}
}
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod async_closures;
mod async_fn_in_trait;
mod autorefs;
pub mod builtin;
mod c_void_returns;
mod context;
mod dangling;
mod default_could_be_derived;
Expand Down Expand Up @@ -86,6 +87,7 @@ use async_closures::AsyncClosureUsage;
use async_fn_in_trait::AsyncFnInTrait;
use autorefs::*;
use builtin::*;
use c_void_returns::*;
use dangling::*;
use default_could_be_derived::DefaultCouldBeDerived;
use deref_into_dyn_supertrait::*;
Expand Down Expand Up @@ -269,6 +271,7 @@ late_lint_methods!(
LifetimeSyntax: LifetimeSyntax,
InternalEqTraitMethodImpls: InternalEqTraitMethodImpls,
ImplicitProvenanceCasts: ImplicitProvenanceCasts,
CVoidReturns: CVoidReturns,
]
]
);
Expand Down
27 changes: 27 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,33 @@ pub(crate) enum BuiltinSpecialModuleNameUsed {
Main,
}

// c_void_return.rs
#[derive(Diagnostic)]
#[diag("`c_void` should not be used as a return type")]
#[help("returning `()` in Rust is equivalent to returning `void` in C")]
pub(crate) struct CVoidReturn {
#[suggestion(
"remove the return type to implicitly return `()`",
code = "",
applicability = "maybe-incorrect"
)]
pub suggestion: Span,
}

// c_void_return.rs
#[derive(Diagnostic)]
#[diag("declarations returning `c_void` are not compatible with C functions returning `void`")]
#[help("returning `()` in Rust is equivalent to returning `void` in C")]
#[note("`c_void` is only used through raw pointers for compatibility with `void` pointers")]
pub(crate) struct ExternCVoidReturn {
#[suggestion(
"remove the return type to implicitly return `()`",
code = "",
applicability = "maybe-incorrect"
)]
pub suggestion: Span,
}

// deref_into_dyn_supertrait.rs
#[derive(Diagnostic)]
#[diag("this `Deref` implementation is covered by an implicit supertrait coercion")]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ rustc_feature = { path = "../rustc_feature" }
rustc_fs_util = { path = "../rustc_fs_util" }
rustc_hir = { path = "../rustc_hir" }
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
rustc_incremental = { path = "../rustc_incremental" }
rustc_index = { path = "../rustc_index" }
rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2472,10 +2472,10 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
&& tcx.dep_graph.try_mark_green(tcx, &dep_node).is_some()
{
let saved_path = &work_product.saved_files["rmeta"];
let incr_comp_session_dir = tcx.sess.incr_comp_session_dir_opt().unwrap();
let source_file = rustc_incremental::in_incr_comp_dir(&incr_comp_session_dir, saved_path);
debug!("copying preexisting metadata from {source_file:?} to {path:?}");
match rustc_fs_util::link_or_copy(&source_file, path) {
let incr_comp_session_dir = tcx.sess.incr_comp_session_dir();
let source_file_in_incr_dir = &incr_comp_session_dir.join(saved_path);
debug!("copying preexisting metadata from {source_file_in_incr_dir:?} to {path:?}");
match rustc_fs_util::link_or_copy(&source_file_in_incr_dir, path) {
Ok(_) => {}
Err(err) => tcx.dcx().emit_fatal(FailCreateFileEncoder { err }),
};
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,12 @@ supported_targets! {
("x86_64-unknown-linux-gnuasan", x86_64_unknown_linux_gnuasan),
("x86_64-unknown-linux-gnumsan", x86_64_unknown_linux_gnumsan),
("x86_64-unknown-linux-gnutsan", x86_64_unknown_linux_gnutsan),

("aarch64-oe-linux-gnu", aarch64_oe_linux_gnu),
("armv7-oe-linux-gnueabihf", armv7_oe_linux_gnueabihf),
("i686-oe-linux-gnu", i686_oe_linux_gnu),
("riscv64-oe-linux-gnu", riscv64_oe_linux_gnu),
("x86_64-oe-linux-gnu", x86_64_oe_linux_gnu),
}

/// Cow-Vec-Str: Cow<'static, [Cow<'static, str>]>
Expand Down
17 changes: 17 additions & 0 deletions compiler/rustc_target/src/spec/targets/aarch64_oe_linux_gnu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::spec::{Target, TargetMetadata};

pub(crate) fn target() -> Target {
let mut base = super::aarch64_unknown_linux_gnu::target();

base.metadata = TargetMetadata {
description: Some("64-bit Linux (kernel 3.2+, glibc 2.17+) for yocto".into()),
tier: Some(3),
host_tools: Some(false),
std: Some(true),
};

base.llvm_target = "aarch64-oe-linux-gnu".into();
base.options.linker = Some("aarch64-oe-linux-gcc".into());

base
}
17 changes: 17 additions & 0 deletions compiler/rustc_target/src/spec/targets/armv7_oe_linux_gnueabihf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::spec::{Target, TargetMetadata};

pub(crate) fn target() -> Target {
let mut base = super::armv7_unknown_linux_gnueabihf::target();

base.metadata = TargetMetadata {
description: Some("Armv7-A Linux, hardfloat (kernel 3.2, glibc 2.17) for yocto".into()),
tier: Some(3),
host_tools: Some(false),
std: Some(true),
};

base.llvm_target = "armv7-oe-linux-gnueabihf".into();
base.options.linker = Some("arm-oe-linux-gnueabi-gcc".into());

base
}
21 changes: 21 additions & 0 deletions compiler/rustc_target/src/spec/targets/i686_oe_linux_gnu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::spec::{Target, TargetMetadata};

pub(crate) fn target() -> Target {
let mut base = super::i686_unknown_linux_gnu::target();

base.metadata = TargetMetadata {
description: Some("32-bit Linux (kernel 3.2, glibc 2.17+) for yocto".into()),
tier: Some(3),
host_tools: Some(false),
std: Some(true),
};

base.llvm_target = "i686-oe-linux-gnu".into();

base.options.linker = Some("i686-oe-linux-gcc".into());

base.options.cpu = "core2".into();
base.options.features = "+sse3".into();

base
}
17 changes: 17 additions & 0 deletions compiler/rustc_target/src/spec/targets/riscv64_oe_linux_gnu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::spec::{Target, TargetMetadata};

pub(crate) fn target() -> Target {
let mut base = super::riscv64gc_unknown_linux_gnu::target();

base.metadata = TargetMetadata {
description: Some("RISC-V Linux (kernel 4.20, glibc 2.29) for yocto".into()),
tier: Some(3),
host_tools: Some(false),
std: Some(true),
};

base.llvm_target = "riscv64-oe-linux-gnu".into();
base.options.linker = Some("riscv64-oe-linux-gcc".into());

base
}
17 changes: 17 additions & 0 deletions compiler/rustc_target/src/spec/targets/x86_64_oe_linux_gnu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::spec::{Target, TargetMetadata};

pub(crate) fn target() -> Target {
let mut base = super::x86_64_unknown_linux_gnu::target();

base.metadata = TargetMetadata {
description: Some("64-bit Linux (kernel 3.2+, glibc 2.17+) for yocto".into()),
tier: Some(3),
host_tools: Some(false),
std: Some(true),
};

base.llvm_target = "x86_64-oe-linux-gnu".into();
base.options.linker = Some("x86_64-oe-linux-gcc".into());

base
}
Loading
Loading