Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 0 additions & 5 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,11 +945,6 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
None,
"Span must not be empty and have no suggestion",
);
debug_assert_eq!(
parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
None,
"suggestion must not have overlapping parts",
);

self.push_suggestion(CodeSuggestion {
substitutions: vec![Substitution { parts }],
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2354,7 +2354,6 @@ impl HumanEmitter {
.sum();
let underline_start = (span_start_pos + start) as isize + offset;
let underline_end = (span_start_pos + start + sub_len) as isize + offset;
assert!(underline_start >= 0 && underline_end >= 0);
let padding: usize = max_line_num_len + 3;
for p in underline_start..underline_end {
if let DisplaySuggestion::Underline = show_code_change
Expand Down
27 changes: 17 additions & 10 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,17 @@ impl CodeSuggestion {
// Assumption: all spans are in the same file, and all spans
// are disjoint. Sort in ascending order.
substitution.parts.sort_by_key(|part| part.span.lo());
// Verify the assumption that all spans are disjoint
assert_eq!(
substitution.parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
None,
"all spans must be disjoint",
);

// Account for cases where we are suggesting the same code that's already
// there. This shouldn't happen often, but in some cases for multipart
// suggestions it's much easier to handle it here than in the origin.
substitution.parts.retain(|p| is_different(sm, &p.snippet, p.span));

// Find the bounding span.
let lo = substitution.parts.iter().map(|part| part.span.lo()).min()?;
Expand Down Expand Up @@ -470,16 +481,12 @@ impl CodeSuggestion {
_ => 1,
})
.sum();
if !is_different(sm, &part.snippet, part.span) {
// Account for cases where we are suggesting the same code that's already
// there. This shouldn't happen often, but in some cases for multipart
// suggestions it's much easier to handle it here than in the origin.
} else {
line_highlight.push(SubstitutionHighlight {
start: (cur_lo.col.0 as isize + acc) as usize,
end: (cur_lo.col.0 as isize + acc + len) as usize,
});
}

line_highlight.push(SubstitutionHighlight {
start: (cur_lo.col.0 as isize + acc) as usize,
end: (cur_lo.col.0 as isize + acc + len) as usize,
});

buf.push_str(&part.snippet);
let cur_hi = sm.lookup_char_pos(part.span.hi());
// Account for the difference between the width of the current code and the
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///! Definition of `InferCtxtLike` from the librarified type layer.
//! Definition of `InferCtxtLike` from the librarified type layer.
use rustc_hir::def_id::DefId;
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::relate::RelateResult;
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ pub(super) fn mangle<'tcx>(
}

pub fn mangle_internal_symbol<'tcx>(tcx: TyCtxt<'tcx>, item_name: &str) -> String {
if item_name == "rust_eh_personality" {
match item_name {
// rust_eh_personality must not be renamed as LLVM hard-codes the name
return "rust_eh_personality".to_owned();
"rust_eh_personality" => return item_name.to_owned(),
// Apple availability symbols need to not be mangled to be usable by
// C/Objective-C code.
"__isPlatformVersionAtLeast" | "__isOSVersionAtLeast" => return item_name.to_owned(),
_ => {}
}

let prefix = "_R";
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@
#![feature(hasher_prefixfree_extras)]
#![feature(hashmap_internals)]
#![feature(hint_must_use)]
#![feature(int_from_ascii)]
#![feature(ip)]
#![feature(lazy_get)]
#![feature(maybe_uninit_slice)]
Expand All @@ -369,6 +370,7 @@
#![feature(slice_internals)]
#![feature(slice_ptr_get)]
#![feature(slice_range)]
#![feature(slice_split_once)]
#![feature(std_internals)]
#![feature(str_internals)]
#![feature(sync_unsafe_cell)]
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod io;
pub mod net;
pub mod os_str;
pub mod path;
pub mod platform_version;
pub mod process;
pub mod random;
pub mod stdio;
Expand Down
180 changes: 180 additions & 0 deletions library/std/src/sys/platform_version/darwin/core_foundation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
//! Minimal utilities for interfacing with a dynamically loaded CoreFoundation.
#![allow(non_snake_case, non_upper_case_globals)]
use super::root_relative;
use crate::ffi::{CStr, c_char, c_void};
use crate::ptr::null_mut;
use crate::sys::common::small_c_string::run_path_with_cstr;

// MacTypes.h
pub(super) type Boolean = u8;
// CoreFoundation/CFBase.h
pub(super) type CFTypeID = usize;
pub(super) type CFOptionFlags = usize;
pub(super) type CFIndex = isize;
pub(super) type CFTypeRef = *mut c_void;
pub(super) type CFAllocatorRef = CFTypeRef;
pub(super) const kCFAllocatorDefault: CFAllocatorRef = null_mut();
// CoreFoundation/CFError.h
pub(super) type CFErrorRef = CFTypeRef;
// CoreFoundation/CFData.h
pub(super) type CFDataRef = CFTypeRef;
// CoreFoundation/CFPropertyList.h
pub(super) const kCFPropertyListImmutable: CFOptionFlags = 0;
pub(super) type CFPropertyListFormat = CFIndex;
pub(super) type CFPropertyListRef = CFTypeRef;
// CoreFoundation/CFString.h
pub(super) type CFStringRef = CFTypeRef;
pub(super) type CFStringEncoding = u32;
pub(super) const kCFStringEncodingUTF8: CFStringEncoding = 0x08000100;
// CoreFoundation/CFDictionary.h
pub(super) type CFDictionaryRef = CFTypeRef;

/// An open handle to the dynamically loaded CoreFoundation framework.
///
/// This is `dlopen`ed, and later `dlclose`d. This is done to try to avoid
/// "leaking" the CoreFoundation symbols to the rest of the user's binary if
/// they decided to not link CoreFoundation themselves.
///
/// It is also faster to look up symbols directly via this handle than with
/// `RTLD_DEFAULT`.
pub(super) struct CFHandle(*mut c_void);

macro_rules! dlsym_fn {
(
unsafe fn $name:ident($($param:ident: $param_ty:ty),* $(,)?) $(-> $ret:ty)?;
) => {
pub(super) unsafe fn $name(&self, $($param: $param_ty),*) $(-> $ret)? {
let ptr = unsafe {
libc::dlsym(
self.0,
concat!(stringify!($name), '\0').as_bytes().as_ptr().cast(),
)
};
if ptr.is_null() {
let err = unsafe { CStr::from_ptr(libc::dlerror()) };
panic!("could not find function {}: {err:?}", stringify!($name));
}

// SAFETY: Just checked that the symbol isn't NULL, and macro invoker verifies that
// the signature is correct.
let fnptr = unsafe {
crate::mem::transmute::<
*mut c_void,
unsafe extern "C" fn($($param_ty),*) $(-> $ret)?,
>(ptr)
};

// SAFETY: Upheld by caller.
unsafe { fnptr($($param),*) }
}
};
}

impl CFHandle {
/// Link to the CoreFoundation dylib, and look up symbols from that.
pub(super) fn new() -> Self {
// We explicitly use non-versioned path here, to allow this to work on older iOS devices.
let cf_path =
root_relative("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation");

let handle = run_path_with_cstr(&cf_path, &|path| unsafe {
Ok(libc::dlopen(path.as_ptr(), libc::RTLD_LAZY | libc::RTLD_LOCAL))
})
.expect("failed allocating string");

if handle.is_null() {
let err = unsafe { CStr::from_ptr(libc::dlerror()) };
panic!("could not open CoreFoundation.framework: {err:?}");
}

Self(handle)
}

pub(super) fn kCFAllocatorNull(&self) -> CFAllocatorRef {
// Available: in all CF versions.
let static_ptr = unsafe { libc::dlsym(self.0, c"kCFAllocatorNull".as_ptr()) };
if static_ptr.is_null() {
let err = unsafe { CStr::from_ptr(libc::dlerror()) };
panic!("could not find kCFAllocatorNull: {err:?}");
}
unsafe { *static_ptr.cast() }
}

// CoreFoundation/CFBase.h
dlsym_fn!(
// Available: in all CF versions.
unsafe fn CFRelease(cf: CFTypeRef);
);
dlsym_fn!(
// Available: in all CF versions.
unsafe fn CFGetTypeID(cf: CFTypeRef) -> CFTypeID;
);

// CoreFoundation/CFData.h
dlsym_fn!(
// Available: in all CF versions.
unsafe fn CFDataCreateWithBytesNoCopy(
allocator: CFAllocatorRef,
bytes: *const u8,
length: CFIndex,
bytes_deallocator: CFAllocatorRef,
) -> CFDataRef;
);

// CoreFoundation/CFPropertyList.h
dlsym_fn!(
// Available: since macOS 10.6.
unsafe fn CFPropertyListCreateWithData(
allocator: CFAllocatorRef,
data: CFDataRef,
options: CFOptionFlags,
format: *mut CFPropertyListFormat,
error: *mut CFErrorRef,
) -> CFPropertyListRef;
);

// CoreFoundation/CFString.h
dlsym_fn!(
// Available: in all CF versions.
unsafe fn CFStringGetTypeID() -> CFTypeID;
);
dlsym_fn!(
// Available: in all CF versions.
unsafe fn CFStringCreateWithCStringNoCopy(
alloc: CFAllocatorRef,
c_str: *const c_char,
encoding: CFStringEncoding,
contents_deallocator: CFAllocatorRef,
) -> CFStringRef;
);
dlsym_fn!(
// Available: in all CF versions.
unsafe fn CFStringGetCString(
the_string: CFStringRef,
buffer: *mut c_char,
buffer_size: CFIndex,
encoding: CFStringEncoding,
) -> Boolean;
);

// CoreFoundation/CFDictionary.h
dlsym_fn!(
// Available: in all CF versions.
unsafe fn CFDictionaryGetTypeID() -> CFTypeID;
);
dlsym_fn!(
// Available: in all CF versions.
unsafe fn CFDictionaryGetValue(
the_dict: CFDictionaryRef,
key: *const c_void,
) -> *const c_void;
);
}

impl Drop for CFHandle {
fn drop(&mut self) {
// Ignore errors when closing. This is also what `libloading` does:
// https://docs.rs/libloading/0.8.6/src/libloading/os/unix/mod.rs.html#374
let _ = unsafe { libc::dlclose(self.0) };
}
}
Loading
Loading