Skip to content

Commit

Permalink
Auto merge of rust-lang#134966 - matthiaskrgr:rollup-lmhmgsv, r=matth…
Browse files Browse the repository at this point in the history
…iaskrgr

Rollup of 4 pull requests

Successful merges:

 - rust-lang#134610 (Format `build.toml` consistently in platform support docs)
 - rust-lang#134918 (Windows: Enable issue 70093 link tests)
 - rust-lang#134953 (Fix doc for read&write unaligned in zst operation)
 - rust-lang#134956 (Account for C string literals and `format_args` in `HiddenUnicodeCodepoints` lint)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Dec 31, 2024
2 parents 7a0cde9 + 0c94f63 commit 34719e8
Show file tree
Hide file tree
Showing 23 changed files with 205 additions and 76 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_ast/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustc_span::{Ident, Span, Symbol};

use crate::Expr;
use crate::ptr::P;
use crate::token::LitKind;

// Definitions:
//
Expand Down Expand Up @@ -45,6 +46,10 @@ pub struct FormatArgs {
pub span: Span,
pub template: Vec<FormatArgsPiece>,
pub arguments: FormatArguments,
/// The raw, un-split format string literal, with no escaping or processing.
///
/// Generally only useful for lints that care about the raw bytes the user wrote.
pub uncooked_fmt_str: (LitKind, Symbol),
}

/// A piece of a format template string.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,7 @@ fn walk_inline_asm_sym<T: MutVisitor>(

fn walk_format_args<T: MutVisitor>(vis: &mut T, fmt: &mut FormatArgs) {
// FIXME: visit the template exhaustively.
let FormatArgs { span, template: _, arguments } = fmt;
let FormatArgs { span, template: _, arguments, uncooked_fmt_str: _ } = fmt;
for FormatArgument { kind, expr } in arguments.all_args_mut() {
match kind {
FormatArgumentKind::Named(ident) | FormatArgumentKind::Captured(ident) => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ pub fn walk_inline_asm_sym<'a, V: Visitor<'a>>(
}

pub fn walk_format_args<'a, V: Visitor<'a>>(visitor: &mut V, fmt: &'a FormatArgs) -> V::Result {
let FormatArgs { span: _, template: _, arguments } = fmt;
let FormatArgs { span: _, template: _, arguments, uncooked_fmt_str: _ } = fmt;
for FormatArgument { kind, expr } in arguments.all_args() {
match kind {
FormatArgumentKind::Named(ident) | FormatArgumentKind::Captured(ident) => {
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_builtin_macros/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use smallvec::smallvec;
use {rustc_ast as ast, rustc_parse_format as parse};

use crate::errors;
use crate::util::expr_to_spanned_string;
use crate::util::{ExprToSpannedString, expr_to_spanned_string};

pub struct AsmArgs {
pub templates: Vec<P<ast::Expr>>,
Expand Down Expand Up @@ -527,7 +527,12 @@ fn expand_preparsed_asm(
let msg = "asm template must be a string literal";
let template_sp = template_expr.span;
let template_is_mac_call = matches!(template_expr.kind, ast::ExprKind::MacCall(_));
let (template_str, template_style, template_span) = {
let ExprToSpannedString {
symbol: template_str,
style: template_style,
span: template_span,
..
} = {
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, template_expr, msg) else {
return ExpandResult::Retry(());
};
Expand Down
18 changes: 14 additions & 4 deletions compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc_parse_format as parse;
use rustc_span::{BytePos, ErrorGuaranteed, Ident, InnerSpan, Span, Symbol};

use crate::errors;
use crate::util::expr_to_spanned_string;
use crate::util::{ExprToSpannedString, expr_to_spanned_string};

// The format_args!() macro is expanded in three steps:
// 1. First, `parse_args` will parse the `(literal, arg, arg, name=arg, name=arg)` syntax,
Expand Down Expand Up @@ -166,13 +166,18 @@ fn make_format_args(

let MacroInput { fmtstr: efmt, mut args, is_direct_literal } = input;

let (fmt_str, fmt_style, fmt_span) = {
let ExprToSpannedString {
symbol: fmt_str,
span: fmt_span,
style: fmt_style,
uncooked_symbol: uncooked_fmt_str,
} = {
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, efmt.clone(), msg) else {
return ExpandResult::Retry(());
};
match mac {
Ok(mut fmt) if append_newline => {
fmt.0 = Symbol::intern(&format!("{}\n", fmt.0));
fmt.symbol = Symbol::intern(&format!("{}\n", fmt.symbol));
fmt
}
Ok(fmt) => fmt,
Expand Down Expand Up @@ -584,7 +589,12 @@ fn make_format_args(
}
}

ExpandResult::Ready(Ok(FormatArgs { span: fmt_span, template, arguments: args }))
ExpandResult::Ready(Ok(FormatArgs {
span: fmt_span,
template,
arguments: args,
uncooked_fmt_str,
}))
}

fn invalid_placeholder_type_error(
Expand Down
23 changes: 19 additions & 4 deletions compiler/rustc_builtin_macros/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ pub(crate) fn warn_on_duplicate_attribute(ecx: &ExtCtxt<'_>, item: &Annotatable,

/// `Ok` represents successfully retrieving the string literal at the correct
/// position, e.g., `println("abc")`.
type ExprToSpannedStringResult<'a> = Result<(Symbol, ast::StrStyle, Span), UnexpectedExprKind<'a>>;
pub(crate) type ExprToSpannedStringResult<'a> = Result<ExprToSpannedString, UnexpectedExprKind<'a>>;

pub(crate) struct ExprToSpannedString {
pub symbol: Symbol,
pub style: ast::StrStyle,
pub span: Span,
/// The raw string literal, with no escaping or processing.
///
/// Generally only useful for lints that care about the raw bytes the user wrote.
pub uncooked_symbol: (ast::token::LitKind, Symbol),
}

/// - `Ok` is returned when the conversion to a string literal is unsuccessful,
/// but another type of expression is obtained instead.
Expand Down Expand Up @@ -90,7 +100,12 @@ pub(crate) fn expr_to_spanned_string<'a>(
ExpandResult::Ready(Err(match expr.kind {
ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
Ok(ast::LitKind::Str(s, style)) => {
return ExpandResult::Ready(Ok((s, style, expr.span)));
return ExpandResult::Ready(Ok(ExprToSpannedString {
symbol: s,
style,
span: expr.span,
uncooked_symbol: (token_lit.kind, token_lit.symbol),
}));
}
Ok(ast::LitKind::ByteStr(..)) => {
let mut err = cx.dcx().struct_span_err(expr.span, err_msg);
Expand Down Expand Up @@ -128,7 +143,7 @@ pub(crate) fn expr_to_string(
Ok((err, _)) => err.emit(),
Err(guar) => guar,
})
.map(|(symbol, style, _)| (symbol, style))
.map(|ExprToSpannedString { symbol, style, .. }| (symbol, style))
})
}

Expand Down Expand Up @@ -183,7 +198,7 @@ pub(crate) fn get_single_str_spanned_from_tts(
Ok((err, _)) => err.emit(),
Err(guar) => guar,
})
.map(|(symbol, _style, span)| (symbol, span))
.map(|ExprToSpannedString { symbol, span, .. }| (symbol, span))
})
}

Expand Down
46 changes: 34 additions & 12 deletions compiler/rustc_lint/src/hidden_unicode_codepoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,36 @@ impl HiddenUnicodeCodepoints {
sub,
});
}

fn check_literal(
&mut self,
cx: &EarlyContext<'_>,
text: Symbol,
lit_kind: ast::token::LitKind,
span: Span,
label: &'static str,
) {
if !contains_text_flow_control_chars(text.as_str()) {
return;
}
let (padding, point_at_inner_spans) = match lit_kind {
// account for `"` or `'`
ast::token::LitKind::Str | ast::token::LitKind::Char => (1, true),
// account for `c"`
ast::token::LitKind::CStr => (2, true),
// account for `r###"`
ast::token::LitKind::StrRaw(n) => (n as u32 + 2, true),
// account for `cr###"`
ast::token::LitKind::CStrRaw(n) => (n as u32 + 3, true),
// suppress bad literals.
ast::token::LitKind::Err(_) => return,
// Be conservative just in case new literals do support these.
_ => (0, false),
};
self.lint_text_direction_codepoint(cx, text, span, padding, point_at_inner_spans, label);
}
}

impl EarlyLintPass for HiddenUnicodeCodepoints {
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
if let ast::AttrKind::DocComment(_, comment) = attr.kind {
Expand All @@ -97,18 +126,11 @@ impl EarlyLintPass for HiddenUnicodeCodepoints {
// byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString`
match &expr.kind {
ast::ExprKind::Lit(token_lit) => {
let text = token_lit.symbol;
if !contains_text_flow_control_chars(text.as_str()) {
return;
}
let padding = match token_lit.kind {
// account for `"` or `'`
ast::token::LitKind::Str | ast::token::LitKind::Char => 1,
// account for `r###"`
ast::token::LitKind::StrRaw(n) => n as u32 + 2,
_ => return,
};
self.lint_text_direction_codepoint(cx, text, expr.span, padding, true, "literal");
self.check_literal(cx, token_lit.symbol, token_lit.kind, expr.span, "literal");
}
ast::ExprKind::FormatArgs(args) => {
let (lit_kind, text) = args.uncooked_fmt_str;
self.check_literal(cx, text, lit_kind, args.span, "format string");
}
_ => {}
};
Expand Down
4 changes: 0 additions & 4 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1403,8 +1403,6 @@ pub const unsafe fn read<T>(src: *const T) -> T {
/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the returned
/// value and the value at `*src` can [violate memory safety][read-ownership].
///
/// Note that even if `T` has size `0`, the pointer must be non-null.
///
/// [read-ownership]: read#ownership-of-the-returned-value
/// [valid]: self#safety
///
Expand Down Expand Up @@ -1611,8 +1609,6 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
///
/// * `dst` must be [valid] for writes.
///
/// Note that even if `T` has size `0`, the pointer must be non-null.
///
/// [valid]: self#safety
///
/// ## On `packed` structs
Expand Down
2 changes: 1 addition & 1 deletion src/doc/rustc/src/platform-support/arm64e-apple-darwin.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ You can build Rust with support for the targets by adding it to the `target` lis

```toml
[build]
target = [ "arm64e-apple-darwin" ]
target = ["arm64e-apple-darwin"]
```

## Building Rust programs
Expand Down
2 changes: 1 addition & 1 deletion src/doc/rustc/src/platform-support/arm64e-apple-ios.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ You can build Rust with support for the targets by adding it to the `target` lis

```toml
[build]
target = [ "arm64e-apple-ios" ]
target = ["arm64e-apple-ios"]
```

## Building Rust programs
Expand Down
2 changes: 1 addition & 1 deletion src/doc/rustc/src/platform-support/arm64e-apple-tvos.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ You can build Rust with support for the targets by adding it to the `target` lis

```toml
[build]
target = [ "arm64e-apple-tvos" ]
target = ["arm64e-apple-tvos"]
```

## Building Rust programs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ list in `config.toml`:

```toml
[build]
target = [ "arm64ec-pc-windows-msvc" ]
target = ["arm64ec-pc-windows-msvc"]
```

## Building Rust programs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ target list in `config.toml`, a sample configuration is shown below.

```toml
[build]
target = [ "hexagon-unknown-linux-musl"]
target = ["hexagon-unknown-linux-musl"]

[target.hexagon-unknown-linux-musl]

Expand Down
2 changes: 1 addition & 1 deletion src/doc/rustc/src/platform-support/unikraft-linux-musl.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ You can build Rust with support for the targets by adding it to the `target` lis
```toml
[build]
build-stage = 1
target = [ "x86_64-unikraft-linux-musl" ]
target = ["x86_64-unikraft-linux-musl"]
```

## Building Rust programs
Expand Down
2 changes: 1 addition & 1 deletion src/doc/rustc/src/platform-support/win7-windows-msvc.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ You can build Rust with support for the targets by adding it to the target list
```toml
[build]
build-stage = 1
target = [ "x86_64-win7-windows-msvc" ]
target = ["x86_64-win7-windows-msvc"]
```

## Building Rust programs
Expand Down
2 changes: 1 addition & 1 deletion src/doc/rustc/src/target-tier-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ To propose addition of a new target, open a pull request on [`rust-lang/rust`]:
Link to the created description page.
- Ensure the pull request is assigned to a member of the [Rust compiler team][rust_compiler_team] by commenting:
```text
r? compiler-team
r? compiler
```

[tier3example]: https://github.com/rust-lang/rust/pull/94872
Expand Down
2 changes: 0 additions & 2 deletions src/tools/tidy/src/issues.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2710,8 +2710,6 @@ ui/limits/issue-75158-64.rs
ui/link-native-libs/issue-109144.rs
ui/link-native-libs/issue-43925.rs
ui/link-native-libs/issue-43926.rs
ui/link-native-libs/issue-70093/issue-70093-link-directives.rs
ui/link-native-libs/issue-70093/issue-70093.rs
ui/linkage-attr/auxiliary/issue-12133-dylib.rs
ui/linkage-attr/auxiliary/issue-12133-dylib2.rs
ui/linkage-attr/auxiliary/issue-12133-rlib.rs
Expand Down

This file was deleted.

10 changes: 0 additions & 10 deletions tests/ui/link-native-libs/issue-70093/issue-70093.rs

This file was deleted.

14 changes: 14 additions & 0 deletions tests/ui/link-native-libs/issue-70093/link-directives.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Ensure that `#[link]` attributes are entirely ignore when using `-Zlink-directives=no`.

//@ run-pass
//@ compile-flags: -Zlink-directives=no
//@ ignore-fuchsia - missing __libc_start_main for some reason (#84733)
//@ ignore-cross-compile - default-linker-libraries=yes doesn't play well with cross compiling

// Usually these `#[link]` attribute would cause `libsome-random-non-existent-library`
// to be passed to the linker, causing it to fail because the file doesn't exist.
// However, with -Zlink-directives=no, the `#[link]` is ignored.
#[link(name = "some-random-non-existent-library", kind = "static")]
extern "C" {}

fn main() {}
26 changes: 26 additions & 0 deletions tests/ui/link-native-libs/issue-70093/link-native-libraries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Ensure that rust does not pass native libraries to the linker when
// `-Zlink-native-libraries=no` is used.

//@ run-pass
//@ compile-flags: -Zlink-native-libraries=no -Cdefault-linker-libraries=yes
//@ ignore-fuchsia - missing __libc_start_main for some reason (#84733)
//@ ignore-cross-compile - default-linker-libraries=yes doesn't play well with cross compiling

//@ revisions: other
//@[other] ignore-msvc

//@ revisions: msvc
// On Windows MSVC, default-linker-libraries=yes doesn't work because
// rustc drives the linker directly instead of going through another compiler.
// Therefore rustc would need to implement default-linker-libraries itself but doesn't.
// So instead we use -Clink-arg to directly set the required msvcrt.lib as a link arg.
//@[msvc] compile-flags: -Clink-arg=msvcrt.lib
//@[msvc] only-msvc

// Usually these `#[link]` attribute would cause `libsome-random-non-existent-library`
// to be passed to the linker, causing it to fail because the file doesn't exist.
// However, -Zlink-native-libraries=no disables that.
#[link(name = "some-random-non-existent-library", kind = "static")]
extern "C" {}

fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/parser/unicode-control-codepoints.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ edition: 2021

fn main() {
// if access_level != "us‫e‪r" { // Check if admin
//~^ ERROR unicode codepoint changing visible direction of text present in comment
Expand Down Expand Up @@ -25,6 +27,14 @@ fn main() {
//~| ERROR non-ASCII character in raw byte string literal
println!("{:?}", '‮');
//~^ ERROR unicode codepoint changing visible direction of text present in literal

let _ = c"‮";
//~^ ERROR unicode codepoint changing visible direction of text present in literal
let _ = cr#"‮"#;
//~^ ERROR unicode codepoint changing visible direction of text present in literal

println!("{{‮}}");
//~^ ERROR unicode codepoint changing visible direction of text present in format string
}

//"/*‮ } ⁦if isAdmin⁩ ⁦ begin admins only */"
Expand Down
Loading

0 comments on commit 34719e8

Please sign in to comment.