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 7 pull requests #111001

Merged
merged 20 commits into from
Apr 30, 2023
Merged
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
3938541
tests: add test for warning-free builds of `core` under `no_global_oo…
ojeda Apr 21, 2023
73b6574
Fix Unreadable non-UTF-8 output on localized MSVC
ChrisDenton Apr 20, 2023
9b9d39e
Abstract `MultiByteToWideChar`
ChrisDenton Apr 27, 2023
107d480
improve error notes for packed struct reference diagnostic
bindsdev Apr 29, 2023
344dd0e
Make `repr` attribute local_only
GuillaumeGomez Apr 28, 2023
63028ac
Do not force anonymous lifetimes in consts to be static.
cjgillot Apr 29, 2023
5fa9751
Move some rustdoc-ui tests to subdirectories
jyn514 Apr 29, 2023
5da288f
move lint tests into subdirectories
jyn514 Apr 29, 2023
57aac3f
Improve internal field comments on `slice::Iter(Mut)`
scottmcm Apr 29, 2023
61b6f65
Get `repr` information through `AdtDef` for foreign items
GuillaumeGomez Apr 28, 2023
89b0956
Fix display of attributes for enums
GuillaumeGomez Apr 29, 2023
2693e20
Extend foreign inlined item with `#[repr()]` test
GuillaumeGomez Apr 29, 2023
b778688
Unify attributes retrieval for JSON and HTML rendering
GuillaumeGomez Apr 29, 2023
734e866
Rollup merge of #110586 - ChrisDenton:msvc-oem-output, r=workingjubilee
matthiaskrgr Apr 29, 2023
e4daa81
Rollup merge of #110652 - ojeda:core-no_global_oom_handling-test, r=M…
matthiaskrgr Apr 29, 2023
791d33c
Rollup merge of #110973 - bindsdev:packed-struct-ref-diagnostic-note,…
matthiaskrgr Apr 29, 2023
4b79276
Rollup merge of #110981 - jyn514:rustdoc-tests, r=notriddle
matthiaskrgr Apr 29, 2023
549b3a1
Rollup merge of #110983 - GuillaumeGomez:foreign-repr, r=notriddle
matthiaskrgr Apr 29, 2023
a656a20
Rollup merge of #110984 - cjgillot:const-infer-lifetime, r=compiler-e…
matthiaskrgr Apr 29, 2023
f720813
Rollup merge of #110997 - scottmcm:slice-iter-comments, r=the8472
matthiaskrgr Apr 29, 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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3250,6 +3250,7 @@ dependencies = [
"tempfile",
"thorin-dwp",
"tracing",
"windows 0.46.0",
]

[[package]]
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_codegen_ssa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ libc = "0.2.50"
version = "0.30.1"
default-features = false
features = ["read_core", "elf", "macho", "pe", "unaligned", "archive", "write"]

[target.'cfg(windows)'.dependencies.windows]
version = "0.46.0"
features = ["Win32_Globalization"]
79 changes: 78 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ fn link_natively<'a>(
if !prog.status.success() {
let mut output = prog.stderr.clone();
output.extend_from_slice(&prog.stdout);
let escaped_output = escape_string(&output);
let escaped_output = escape_linker_output(&output, flavor);
// FIXME: Add UI tests for this error.
let err = errors::LinkingFailed {
linker_path: &linker_path,
Expand Down Expand Up @@ -1052,6 +1052,83 @@ fn escape_string(s: &[u8]) -> String {
}
}

#[cfg(not(windows))]
fn escape_linker_output(s: &[u8], _flavour: LinkerFlavor) -> String {
escape_string(s)
}

/// If the output of the msvc linker is not UTF-8 and the host is Windows,
/// then try to convert the string from the OEM encoding.
#[cfg(windows)]
fn escape_linker_output(s: &[u8], flavour: LinkerFlavor) -> String {
// This only applies to the actual MSVC linker.
if flavour != LinkerFlavor::Msvc(Lld::No) {
return escape_string(s);
}
match str::from_utf8(s) {
Ok(s) => return s.to_owned(),
Err(_) => match win::locale_byte_str_to_string(s, win::oem_code_page()) {
Some(s) => s,
// The string is not UTF-8 and isn't valid for the OEM code page
None => format!("Non-UTF-8 output: {}", s.escape_ascii()),
},
}
}

/// Wrappers around the Windows API.
#[cfg(windows)]
mod win {
use windows::Win32::Globalization::{
GetLocaleInfoEx, MultiByteToWideChar, CP_OEMCP, LOCALE_IUSEUTF8LEGACYOEMCP,
LOCALE_NAME_SYSTEM_DEFAULT, LOCALE_RETURN_NUMBER, MB_ERR_INVALID_CHARS,
};

/// Get the Windows system OEM code page. This is most notably the code page
/// used for link.exe's output.
pub fn oem_code_page() -> u32 {
unsafe {
let mut cp: u32 = 0;
// We're using the `LOCALE_RETURN_NUMBER` flag to return a u32.
// But the API requires us to pass the data as though it's a [u16] string.
let len = std::mem::size_of::<u32>() / std::mem::size_of::<u16>();
let data = std::slice::from_raw_parts_mut(&mut cp as *mut u32 as *mut u16, len);
let len_written = GetLocaleInfoEx(
LOCALE_NAME_SYSTEM_DEFAULT,
LOCALE_IUSEUTF8LEGACYOEMCP | LOCALE_RETURN_NUMBER,
Some(data),
);
if len_written as usize == len { cp } else { CP_OEMCP }
}
}
/// Try to convert a multi-byte string to a UTF-8 string using the given code page
/// The string does not need to be null terminated.
///
/// This is implemented as a wrapper around `MultiByteToWideChar`.
/// See <https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar>
///
/// It will fail if the multi-byte string is longer than `i32::MAX` or if it contains
/// any invalid bytes for the expected encoding.
pub fn locale_byte_str_to_string(s: &[u8], code_page: u32) -> Option<String> {
// `MultiByteToWideChar` requires a length to be a "positive integer".
if s.len() > isize::MAX as usize {
return None;
}
// Error if the string is not valid for the expected code page.
let flags = MB_ERR_INVALID_CHARS;
// Call MultiByteToWideChar twice.
// First to calculate the length then to convert the string.
let mut len = unsafe { MultiByteToWideChar(code_page, flags, s, None) };
if len > 0 {
let mut utf16 = vec![0; len as usize];
len = unsafe { MultiByteToWideChar(code_page, flags, s, Some(&mut utf16)) };
if len > 0 {
return utf16.get(..len as usize).map(String::from_utf16_lossy);
}
}
None
}
}

fn add_sanitizer_libraries(sess: &Session, crate_type: CrateType, linker: &mut dyn Linker) {
// On macOS the runtimes are distributed as dylibs which should be linked to
// both executables and dynamic shared objects. Everywhere else the runtimes
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
),
ungated!(link_name, Normal, template!(NameValueStr: "name"), FutureWarnPreceding),
ungated!(no_link, Normal, template!(Word), WarnFollowing),
ungated!(repr, Normal, template!(List: "C"), DuplicatesOk),
ungated!(repr, Normal, template!(List: "C"), DuplicatesOk, @only_local: true),
ungated!(export_name, Normal, template!(NameValueStr: "name"), FutureWarnPreceding),
ungated!(link_section, Normal, template!(NameValueStr: "name"), FutureWarnPreceding),
ungated!(no_mangle, Normal, template!(Word), WarnFollowing, @only_local: true),
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_mir_transform/src/check_packed_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> {
"reference to packed field is unaligned"
)
.note(
"fields of packed structs are not properly aligned, and creating \
a misaligned reference is undefined behavior (even if that \
"packed structs are only aligned by one byte, and many modern architectures \
penalize unaligned field accesses"
)
.note(
"creating a misaligned reference is undefined behavior (even if that \
reference is never dereferenced)",
).help(
"copy the field contents to a local variable, or replace the \
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
fn visit_anon_const(&mut self, constant: &'ast AnonConst) {
// We deal with repeat expressions explicitly in `resolve_expr`.
self.with_lifetime_rib(LifetimeRibKind::AnonConst, |this| {
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
this.resolve_anon_const(constant, IsRepeatExpr::No);
})
})
Expand Down Expand Up @@ -4126,7 +4126,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
ExprKind::Repeat(ref elem, ref ct) => {
self.visit_expr(elem);
self.with_lifetime_rib(LifetimeRibKind::AnonConst, |this| {
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
this.resolve_anon_const(ct, IsRepeatExpr::Yes)
})
});
Expand Down
26 changes: 20 additions & 6 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,17 @@ impl<'a, T> IntoIterator for &'a mut [T] {
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Iter<'a, T: 'a> {
/// The pointer to the next element to return, or the past-the-end location
/// if the iterator is empty.
///
/// This address will be used for all ZST elements, never changed.
ptr: NonNull<T>,
end: *const T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that
// ptr == end is a quick test for the Iterator being empty, that works
// for both ZST and non-ZST.
/// For non-ZSTs, the non-null pointer to the past-the-end element.
///
/// For ZSTs, this is `ptr.wrapping_byte_add(len)`.
///
/// For all types, `ptr == end` tests whether the iterator is empty.
end: *const T,
_marker: PhantomData<&'a T>,
}

Expand Down Expand Up @@ -179,10 +186,17 @@ impl<T> AsRef<[T]> for Iter<'_, T> {
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct IterMut<'a, T: 'a> {
/// The pointer to the next element to return, or the past-the-end location
/// if the iterator is empty.
///
/// This address will be used for all ZST elements, never changed.
ptr: NonNull<T>,
end: *mut T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that
// ptr == end is a quick test for the Iterator being empty, that works
// for both ZST and non-ZST.
/// For non-ZSTs, the non-null pointer to the past-the-end element.
///
/// For ZSTs, this is `ptr.wrapping_byte_add(len)`.
///
/// For all types, `ptr == end` tests whether the iterator is empty.
end: *mut T,
_marker: PhantomData<&'a mut T>,
}

Expand Down
73 changes: 73 additions & 0 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use arrayvec::ArrayVec;
use thin_vec::ThinVec;

use rustc_ast as ast;
use rustc_ast_pretty::pprust;
use rustc_attr::{ConstStability, Deprecation, Stability, StabilityLevel};
use rustc_const_eval::const_eval::is_unstable_const_fn;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -711,6 +712,78 @@ impl Item {
};
Some(tcx.visibility(def_id))
}

pub(crate) fn attributes(&self, tcx: TyCtxt<'_>, keep_as_is: bool) -> Vec<String> {
const ALLOWED_ATTRIBUTES: &[Symbol] =
&[sym::export_name, sym::link_section, sym::no_mangle, sym::repr, sym::non_exhaustive];

use rustc_abi::IntegerType;
use rustc_middle::ty::ReprFlags;

let mut attrs: Vec<String> = self
.attrs
.other_attrs
.iter()
.filter_map(|attr| {
if keep_as_is {
Some(pprust::attribute_to_string(attr))
} else if ALLOWED_ATTRIBUTES.contains(&attr.name_or_empty()) {
Some(
pprust::attribute_to_string(attr)
.replace("\\\n", "")
.replace('\n', "")
.replace(" ", " "),
)
} else {
None
}
})
.collect();
if let Some(def_id) = self.item_id.as_def_id() &&
!def_id.is_local() &&
// This check is needed because `adt_def` will panic if not a compatible type otherwise...
matches!(self.type_(), ItemType::Struct | ItemType::Enum | ItemType::Union)
{
let repr = tcx.adt_def(def_id).repr();
let mut out = Vec::new();
if repr.flags.contains(ReprFlags::IS_C) {
out.push("C");
}
if repr.flags.contains(ReprFlags::IS_TRANSPARENT) {
out.push("transparent");
}
if repr.flags.contains(ReprFlags::IS_SIMD) {
out.push("simd");
}
let pack_s;
if let Some(pack) = repr.pack {
pack_s = format!("packed({})", pack.bytes());
out.push(&pack_s);
}
let align_s;
if let Some(align) = repr.align {
align_s = format!("align({})", align.bytes());
out.push(&align_s);
}
let int_s;
if let Some(int) = repr.int {
int_s = match int {
IntegerType::Pointer(is_signed) => {
format!("{}size", if is_signed { 'i' } else { 'u' })
}
IntegerType::Fixed(size, is_signed) => {
format!("{}{}", if is_signed { 'i' } else { 'u' }, size.size().bytes() * 8)
}
};
out.push(&int_s);
}
if out.is_empty() {
return Vec::new();
}
attrs.push(format!("#[repr({})]", out.join(", ")));
}
attrs
}
}

#[derive(Clone, Debug)]
Expand Down
38 changes: 8 additions & 30 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ use std::str;
use std::string::ToString;

use askama::Template;
use rustc_ast_pretty::pprust;
use rustc_attr::{ConstStability, Deprecation, StabilityLevel};
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -849,10 +848,10 @@ fn assoc_method(
let (indent, indent_str, end_newline) = if parent == ItemType::Trait {
header_len += 4;
let indent_str = " ";
write!(w, "{}", render_attributes_in_pre(meth, indent_str));
write!(w, "{}", render_attributes_in_pre(meth, indent_str, tcx));
(4, indent_str, Ending::NoNewline)
} else {
render_attributes_in_code(w, meth);
render_attributes_in_code(w, meth, tcx);
(0, "", Ending::Newline)
};
w.reserve(header_len + "<a href=\"\" class=\"fn\">{".len() + "</a>".len());
Expand Down Expand Up @@ -1021,36 +1020,15 @@ fn render_assoc_item(
}
}

const ALLOWED_ATTRIBUTES: &[Symbol] =
&[sym::export_name, sym::link_section, sym::no_mangle, sym::repr, sym::non_exhaustive];

fn attributes(it: &clean::Item) -> Vec<String> {
it.attrs
.other_attrs
.iter()
.filter_map(|attr| {
if ALLOWED_ATTRIBUTES.contains(&attr.name_or_empty()) {
Some(
pprust::attribute_to_string(attr)
.replace("\\\n", "")
.replace('\n', "")
.replace(" ", " "),
)
} else {
None
}
})
.collect()
}

// When an attribute is rendered inside a `<pre>` tag, it is formatted using
// a whitespace prefix and newline.
fn render_attributes_in_pre<'a>(
fn render_attributes_in_pre<'a, 'b: 'a>(
it: &'a clean::Item,
prefix: &'a str,
) -> impl fmt::Display + Captures<'a> {
tcx: TyCtxt<'b>,
) -> impl fmt::Display + Captures<'a> + Captures<'b> {
crate::html::format::display_fn(move |f| {
for a in attributes(it) {
for a in it.attributes(tcx, false) {
writeln!(f, "{}{}", prefix, a)?;
}
Ok(())
Expand All @@ -1059,8 +1037,8 @@ fn render_attributes_in_pre<'a>(

// When an attribute is rendered inside a <code> tag, it is formatted using
// a div to produce a newline after it.
fn render_attributes_in_code(w: &mut Buffer, it: &clean::Item) {
for a in attributes(it) {
fn render_attributes_in_code(w: &mut Buffer, it: &clean::Item, tcx: TyCtxt<'_>) {
for a in it.attributes(tcx, false) {
write!(w, "<div class=\"code-attribute\">{}</div>", a);
}
}
Expand Down
Loading