Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 6 pull requests #111093

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
5dd54fd
lint/ctypes: ext. abi fn-ptr in internal abi fn
davidtwco Mar 1, 2023
c1dcf26
abi: avoid ice for non-ffi-safe fn ptrs
davidtwco Mar 1, 2023
92ae35f
lint/ctypes: multiple external fn-ptrs in ty
davidtwco Mar 2, 2023
3e7ca3e
lint/ctypes: check other types for ext. fn-ptr ty
davidtwco Mar 2, 2023
1a7132d
rustdoc-search: fix incorrect doc comment
notriddle Apr 14, 2023
4c11822
rustdoc: restructure type search engine to pick-and-use IDs
notriddle Apr 15, 2023
1ece1ea
Stablize raw-dylib, link_ordinal and -Cdlltool
dpaoliello Mar 27, 2023
b6f81e0
rustdoc-search: give longer notification for type corrections
notriddle Apr 19, 2023
e0a7462
rustdoc-search: clean up `checkIfInGenerics` call at end of `checkType`
notriddle Apr 20, 2023
7529d87
rustdoc-search: make type name correction choice deterministic
notriddle Apr 20, 2023
395840c
rustdoc-search: use more descriptive "x not found; y instead" message
notriddle Apr 20, 2023
d5e7ac5
avoid duplicating TLS state between test std and realstd
RalfJung Apr 28, 2023
ed468ee
Encode def span for foreign RPITITs
compiler-errors Apr 30, 2023
bc68de9
remove pointless `FIXME` in `bootstrap::download`
onur-ozkan May 1, 2023
b3bc5f8
Rollup merge of #108611 - davidtwco:issue-94223-external-abi-fn-ptr-i…
Dylan-DPC May 2, 2023
108a26c
Rollup merge of #109677 - dpaoliello:rawdylib, r=michaelwoerister,wes…
Dylan-DPC May 2, 2023
c9f03bf
Rollup merge of #110371 - notriddle:notriddle/search-corrections, r=G…
Dylan-DPC May 2, 2023
6b3cfaa
Rollup merge of #110946 - RalfJung:tls-realstd, r=m-ou-se
Dylan-DPC May 2, 2023
c6ba892
Rollup merge of #111039 - compiler-errors:foreign-span-rpitit, r=tmiasko
Dylan-DPC May 2, 2023
5c7cd00
Rollup merge of #111069 - ozkanonur:remove-pointless-fixme, r=albertl…
Dylan-DPC May 2, 2023
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
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ codegen_llvm_error_writing_def_file =
Error writing .DEF file: {$error}
codegen_llvm_error_calling_dlltool =
Error calling dlltool: {$error}
Error calling dlltool '{$dlltool_path}': {$error}
codegen_llvm_dlltool_fail_import_library =
Dlltool could not create import library: {$stdout}
Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_codegen_llvm/src/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder {
"arm" => ("arm", "--32"),
_ => panic!("unsupported arch {}", sess.target.arch),
};
let result = std::process::Command::new(dlltool)
let result = std::process::Command::new(&dlltool)
.args([
"-d",
def_file_path.to_str().unwrap(),
Expand All @@ -218,9 +218,13 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder {

match result {
Err(e) => {
sess.emit_fatal(ErrorCallingDllTool { error: e });
sess.emit_fatal(ErrorCallingDllTool {
dlltool_path: dlltool.to_string_lossy(),
error: e,
});
}
Ok(output) if !output.status.success() => {
// dlltool returns '0' on failure, so check for error output instead.
Ok(output) if !output.stderr.is_empty() => {
sess.emit_fatal(DlltoolFailImportLibrary {
stdout: String::from_utf8_lossy(&output.stdout),
stderr: String::from_utf8_lossy(&output.stderr),
Expand Down Expand Up @@ -431,7 +435,7 @@ fn string_to_io_error(s: String) -> io::Error {

fn find_binutils_dlltool(sess: &Session) -> OsString {
assert!(sess.target.options.is_like_windows && !sess.target.options.is_like_msvc);
if let Some(dlltool_path) = &sess.opts.unstable_opts.dlltool {
if let Some(dlltool_path) = &sess.opts.cg.dlltool {
return dlltool_path.clone().into_os_string();
}

Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_llvm/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ pub(crate) struct ErrorWritingDEFFile {

#[derive(Diagnostic)]
#[diag(codegen_llvm_error_calling_dlltool)]
pub(crate) struct ErrorCallingDllTool {
pub(crate) struct ErrorCallingDllTool<'a> {
pub dlltool_path: Cow<'a, str>,
pub error: std::io::Error,
}

Expand Down
9 changes: 0 additions & 9 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,15 +592,6 @@ fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {

fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<u16> {
use rustc_ast::{LitIntType, LitKind, MetaItemLit};
if !tcx.features().raw_dylib && tcx.sess.target.arch == "x86" {
feature_err(
&tcx.sess.parse_sess,
sym::raw_dylib,
attr.span,
"`#[link_ordinal]` is unstable on x86",
)
.emit();
}
let meta_item_list = attr.meta_item_list();
let meta_item_list = meta_item_list.as_deref();
let sole_meta_list = match meta_item_list {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ declare_features! (
(accepted, pub_restricted, "1.18.0", Some(32409), None),
/// Allows use of the postfix `?` operator in expressions.
(accepted, question_mark, "1.13.0", Some(31436), None),
/// Allows the use of raw-dylibs (RFC 2627).
(accepted, raw_dylib, "CURRENT_RUSTC_VERSION", Some(58713), None),
/// Allows keywords to be escaped for use as identifiers.
(accepted, raw_identifiers, "1.30.0", Some(48589), None),
/// Allows relaxing the coherence rules such that
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,6 @@ declare_features! (
(active, precise_pointer_size_matching, "1.32.0", Some(56354), None),
/// Allows macro attributes on expressions, statements and non-inline modules.
(active, proc_macro_hygiene, "1.30.0", Some(54727), None),
/// Allows the use of raw-dylibs (RFC 2627).
(active, raw_dylib, "1.65.0", Some(58713), None),
/// Allows `&raw const $place_expr` and `&raw mut $place_expr` expressions.
(active, raw_ref_op, "1.41.0", Some(64490), None),
/// Allows using the `#[register_tool]` attribute.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ fn test_codegen_options_tracking_hash() {
untracked!(ar, String::from("abc"));
untracked!(codegen_units, Some(42));
untracked!(default_linker_libraries, true);
untracked!(dlltool, Some(PathBuf::from("custom_dlltool.exe")));
untracked!(extra_filename, String::from("extra-filename"));
untracked!(incremental, Some(String::from("abc")));
// `link_arg` is omitted because it just forwards to `link_args`.
Expand Down Expand Up @@ -651,7 +652,6 @@ fn test_unstable_options_tracking_hash() {
untracked!(assert_incr_state, Some(String::from("loaded")));
untracked!(deduplicate_diagnostics, false);
untracked!(dep_tasks, true);
untracked!(dlltool, Some(PathBuf::from("custom_dlltool.exe")));
untracked!(dont_buffer_diagnostics, true);
untracked!(dump_dep_graph, true);
untracked!(dump_drop_tracking_cfg, Some("cfg.dot".to_string()));
Expand Down
166 changes: 149 additions & 17 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,17 +1250,45 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
}
}

fn check_foreign_fn(&mut self, def_id: LocalDefId, decl: &hir::FnDecl<'_>) {
/// Check if a function's argument types and result type are "ffi-safe".
///
/// Argument types and the result type are checked for functions with external ABIs.
/// For functions with internal ABIs, argument types and the result type are walked to find
/// fn-ptr types that have external ABIs, as these still need checked.
fn check_maybe_foreign_fn(
&mut self,
abi: SpecAbi,
def_id: LocalDefId,
decl: &'tcx hir::FnDecl<'_>,
) {
let sig = self.cx.tcx.fn_sig(def_id).subst_identity();
let sig = self.cx.tcx.erase_late_bound_regions(sig);

let is_internal_abi = self.is_internal_abi(abi);
let check_ty = |this: &mut ImproperCTypesVisitor<'a, 'tcx>,
hir_ty: &'tcx hir::Ty<'_>,
ty: Ty<'tcx>,
is_return_type: bool| {
// If this function has an external ABI, then its arguments and return type should be
// checked..
if !is_internal_abi {
this.check_type_for_ffi_and_report_errors(hir_ty.span, ty, false, is_return_type);
return;
}

// ..but if this function has an internal ABI, then search the argument or return type
// for any fn-ptr types with external ABI, which should be checked..
for (fn_ptr_ty, span) in this.find_fn_ptr_ty_with_external_abi(hir_ty, ty) {
this.check_type_for_ffi_and_report_errors(span, fn_ptr_ty, false, is_return_type);
}
};

for (input_ty, input_hir) in iter::zip(sig.inputs(), decl.inputs) {
self.check_type_for_ffi_and_report_errors(input_hir.span, *input_ty, false, false);
check_ty(self, input_hir, *input_ty, false);
}

if let hir::FnRetTy::Return(ref ret_hir) = decl.output {
let ret_ty = sig.output();
self.check_type_for_ffi_and_report_errors(ret_hir.span, ret_ty, false, true);
check_ty(self, ret_hir, sig.output(), true);
}
}

Expand All @@ -1275,28 +1303,134 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
SpecAbi::Rust | SpecAbi::RustCall | SpecAbi::RustIntrinsic | SpecAbi::PlatformIntrinsic
)
}

/// Find any fn-ptr types with external ABIs in `ty`.
///
/// For example, `Option<extern "C" fn()>` returns `extern "C" fn()`
fn find_fn_ptr_ty_with_external_abi(
&self,
hir_ty: &hir::Ty<'tcx>,
ty: Ty<'tcx>,
) -> Vec<(Ty<'tcx>, Span)> {
struct FnPtrFinder<'parent, 'a, 'tcx> {
visitor: &'parent ImproperCTypesVisitor<'a, 'tcx>,
spans: Vec<Span>,
tys: Vec<Ty<'tcx>>,
}

impl<'parent, 'a, 'tcx> hir::intravisit::Visitor<'_> for FnPtrFinder<'parent, 'a, 'tcx> {
fn visit_ty(&mut self, ty: &'_ hir::Ty<'_>) {
debug!(?ty);
if let hir::TyKind::BareFn(hir::BareFnTy { abi, .. }) = ty.kind
&& !self.visitor.is_internal_abi(*abi)
{
self.spans.push(ty.span);
}

hir::intravisit::walk_ty(self, ty)
}
}

impl<'vis, 'a, 'tcx> ty::visit::TypeVisitor<TyCtxt<'tcx>> for FnPtrFinder<'vis, 'a, 'tcx> {
type BreakTy = Ty<'tcx>;

fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
if let ty::FnPtr(sig) = ty.kind() && !self.visitor.is_internal_abi(sig.abi()) {
self.tys.push(ty);
}

ty.super_visit_with(self)
}
}

let mut visitor = FnPtrFinder { visitor: &*self, spans: Vec::new(), tys: Vec::new() };
self.cx
.tcx
.try_normalize_erasing_regions(self.cx.param_env, ty)
.unwrap_or(ty)
.visit_with(&mut visitor);
hir::intravisit::Visitor::visit_ty(&mut visitor, hir_ty);

iter::zip(visitor.tys.drain(..), visitor.spans.drain(..)).collect()
}
}

impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDeclarations {
fn check_foreign_item(&mut self, cx: &LateContext<'_>, it: &hir::ForeignItem<'_>) {
fn check_foreign_item(&mut self, cx: &LateContext<'tcx>, it: &hir::ForeignItem<'tcx>) {
let mut vis = ImproperCTypesVisitor { cx, mode: CItemKind::Declaration };
let abi = cx.tcx.hir().get_foreign_abi(it.hir_id());

if !vis.is_internal_abi(abi) {
match it.kind {
hir::ForeignItemKind::Fn(ref decl, _, _) => {
vis.check_foreign_fn(it.owner_id.def_id, decl);
}
hir::ForeignItemKind::Static(ref ty, _) => {
vis.check_foreign_static(it.owner_id, ty.span);
}
hir::ForeignItemKind::Type => (),
match it.kind {
hir::ForeignItemKind::Fn(ref decl, _, _) => {
vis.check_maybe_foreign_fn(abi, it.owner_id.def_id, decl);
}
hir::ForeignItemKind::Static(ref ty, _) if !vis.is_internal_abi(abi) => {
vis.check_foreign_static(it.owner_id, ty.span);
}
hir::ForeignItemKind::Static(..) | hir::ForeignItemKind::Type => (),
}
}
}

impl ImproperCTypesDefinitions {
fn check_ty_maybe_containing_foreign_fnptr<'tcx>(
&mut self,
cx: &LateContext<'tcx>,
hir_ty: &'tcx hir::Ty<'_>,
ty: Ty<'tcx>,
) {
let mut vis = ImproperCTypesVisitor { cx, mode: CItemKind::Definition };
for (fn_ptr_ty, span) in vis.find_fn_ptr_ty_with_external_abi(hir_ty, ty) {
vis.check_type_for_ffi_and_report_errors(span, fn_ptr_ty, true, false);
}
}
}

/// `ImproperCTypesDefinitions` checks items outside of foreign items (e.g. stuff that isn't in
/// `extern "C" { }` blocks):
///
/// - `extern "<abi>" fn` definitions are checked in the same way as the
/// `ImproperCtypesDeclarations` visitor checks functions if `<abi>` is external (e.g. "C").
/// - All other items which contain types (e.g. other functions, struct definitions, etc) are
/// checked for extern fn-ptrs with external ABIs.
impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
match item.kind {
hir::ItemKind::Static(ty, ..)
| hir::ItemKind::Const(ty, ..)
| hir::ItemKind::TyAlias(ty, ..) => {
self.check_ty_maybe_containing_foreign_fnptr(
cx,
ty,
cx.tcx.type_of(item.owner_id).subst_identity(),
);
}
// See `check_fn`..
hir::ItemKind::Fn(..) => {}
// See `check_field_def`..
hir::ItemKind::Union(..) | hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) => {}
// Doesn't define something that can contain a external type to be checked.
hir::ItemKind::Impl(..)
| hir::ItemKind::TraitAlias(..)
| hir::ItemKind::Trait(..)
| hir::ItemKind::OpaqueTy(..)
| hir::ItemKind::GlobalAsm(..)
| hir::ItemKind::ForeignMod { .. }
| hir::ItemKind::Mod(..)
| hir::ItemKind::Macro(..)
| hir::ItemKind::Use(..)
| hir::ItemKind::ExternCrate(..) => {}
}
}

fn check_field_def(&mut self, cx: &LateContext<'tcx>, field: &'tcx hir::FieldDef<'tcx>) {
self.check_ty_maybe_containing_foreign_fnptr(
cx,
field.ty,
cx.tcx.type_of(field.def_id).subst_identity(),
);
}

fn check_fn(
&mut self,
cx: &LateContext<'tcx>,
Expand All @@ -1315,9 +1449,7 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
};

let mut vis = ImproperCTypesVisitor { cx, mode: CItemKind::Definition };
if !vis.is_internal_abi(abi) {
vis.check_foreign_fn(id, decl);
}
vis.check_maybe_foreign_fn(abi, id, decl);
}
}

Expand Down
18 changes: 0 additions & 18 deletions compiler/rustc_metadata/src/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,6 @@ impl<'tcx> Collector<'tcx> {
"raw-dylib" => {
if !sess.target.is_like_windows {
sess.emit_err(errors::FrameworkOnlyWindows { span });
} else if !features.raw_dylib && sess.target.arch == "x86" {
feature_err(
&sess.parse_sess,
sym::raw_dylib,
span,
"link kind `raw-dylib` is unstable on x86",
)
.emit();
}
NativeLibKind::RawDylib
}
Expand Down Expand Up @@ -251,16 +243,6 @@ impl<'tcx> Collector<'tcx> {
continue;
}
};
if !features.raw_dylib {
let span = item.name_value_literal_span().unwrap();
feature_err(
&sess.parse_sess,
sym::raw_dylib,
span,
"import name type is unstable",
)
.emit();
}
import_name_type = Some((link_import_name_type, item.span()));
}
_ => {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,12 @@ fn should_encode_span(def_kind: DefKind) -> bool {
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy
| DefKind::ImplTraitPlaceholder
| DefKind::Field
| DefKind::Impl { .. }
| DefKind::Closure
| DefKind::Generator => true,
DefKind::ForeignMod | DefKind::ImplTraitPlaceholder | DefKind::GlobalAsm => false,
DefKind::ForeignMod | DefKind::GlobalAsm => false,
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,8 @@ options! {
line-tables-only, limited, or full; default: 0)"),
default_linker_libraries: bool = (false, parse_bool, [UNTRACKED],
"allow the linker to link its default libraries (default: no)"),
dlltool: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
"import library generation tool (ignored except when targeting windows-gnu)"),
embed_bitcode: bool = (true, parse_bool, [TRACKED],
"emit bitcode in rlibs (default: yes)"),
extra_filename: String = (String::new(), parse_string, [UNTRACKED],
Expand Down Expand Up @@ -1391,8 +1393,6 @@ options! {
(default: no)"),
diagnostic_width: Option<usize> = (None, parse_opt_number, [UNTRACKED],
"set the current output width for diagnostic truncation"),
dlltool: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
"import library generation tool (windows-gnu only)"),
dont_buffer_diagnostics: bool = (false, parse_bool, [UNTRACKED],
"emit diagnostics rather than buffering (breaks NLL error downgrading, sorting) \
(default: no)"),
Expand Down
Loading