Skip to content

Commit 34719e8

Browse files
committed
Auto merge of rust-lang#134966 - matthiaskrgr:rollup-lmhmgsv, r=matthiaskrgr
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
2 parents 7a0cde9 + 0c94f63 commit 34719e8

23 files changed

+205
-76
lines changed

Diff for: compiler/rustc_ast/src/format.rs

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_span::{Ident, Span, Symbol};
44

55
use crate::Expr;
66
use crate::ptr::P;
7+
use crate::token::LitKind;
78

89
// Definitions:
910
//
@@ -45,6 +46,10 @@ pub struct FormatArgs {
4546
pub span: Span,
4647
pub template: Vec<FormatArgsPiece>,
4748
pub arguments: FormatArguments,
49+
/// The raw, un-split format string literal, with no escaping or processing.
50+
///
51+
/// Generally only useful for lints that care about the raw bytes the user wrote.
52+
pub uncooked_fmt_str: (LitKind, Symbol),
4853
}
4954

5055
/// A piece of a format template string.

Diff for: compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,7 @@ fn walk_inline_asm_sym<T: MutVisitor>(
15961596

15971597
fn walk_format_args<T: MutVisitor>(vis: &mut T, fmt: &mut FormatArgs) {
15981598
// FIXME: visit the template exhaustively.
1599-
let FormatArgs { span, template: _, arguments } = fmt;
1599+
let FormatArgs { span, template: _, arguments, uncooked_fmt_str: _ } = fmt;
16001600
for FormatArgument { kind, expr } in arguments.all_args_mut() {
16011601
match kind {
16021602
FormatArgumentKind::Named(ident) | FormatArgumentKind::Captured(ident) => {

Diff for: compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ pub fn walk_inline_asm_sym<'a, V: Visitor<'a>>(
10611061
}
10621062

10631063
pub fn walk_format_args<'a, V: Visitor<'a>>(visitor: &mut V, fmt: &'a FormatArgs) -> V::Result {
1064-
let FormatArgs { span: _, template: _, arguments } = fmt;
1064+
let FormatArgs { span: _, template: _, arguments, uncooked_fmt_str: _ } = fmt;
10651065
for FormatArgument { kind, expr } in arguments.all_args() {
10661066
match kind {
10671067
FormatArgumentKind::Named(ident) | FormatArgumentKind::Captured(ident) => {

Diff for: compiler/rustc_builtin_macros/src/asm.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use smallvec::smallvec;
1616
use {rustc_ast as ast, rustc_parse_format as parse};
1717

1818
use crate::errors;
19-
use crate::util::expr_to_spanned_string;
19+
use crate::util::{ExprToSpannedString, expr_to_spanned_string};
2020

2121
pub struct AsmArgs {
2222
pub templates: Vec<P<ast::Expr>>,
@@ -527,7 +527,12 @@ fn expand_preparsed_asm(
527527
let msg = "asm template must be a string literal";
528528
let template_sp = template_expr.span;
529529
let template_is_mac_call = matches!(template_expr.kind, ast::ExprKind::MacCall(_));
530-
let (template_str, template_style, template_span) = {
530+
let ExprToSpannedString {
531+
symbol: template_str,
532+
style: template_style,
533+
span: template_span,
534+
..
535+
} = {
531536
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, template_expr, msg) else {
532537
return ExpandResult::Retry(());
533538
};

Diff for: compiler/rustc_builtin_macros/src/format.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_parse_format as parse;
1717
use rustc_span::{BytePos, ErrorGuaranteed, Ident, InnerSpan, Span, Symbol};
1818

1919
use crate::errors;
20-
use crate::util::expr_to_spanned_string;
20+
use crate::util::{ExprToSpannedString, expr_to_spanned_string};
2121

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

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

169-
let (fmt_str, fmt_style, fmt_span) = {
169+
let ExprToSpannedString {
170+
symbol: fmt_str,
171+
span: fmt_span,
172+
style: fmt_style,
173+
uncooked_symbol: uncooked_fmt_str,
174+
} = {
170175
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, efmt.clone(), msg) else {
171176
return ExpandResult::Retry(());
172177
};
173178
match mac {
174179
Ok(mut fmt) if append_newline => {
175-
fmt.0 = Symbol::intern(&format!("{}\n", fmt.0));
180+
fmt.symbol = Symbol::intern(&format!("{}\n", fmt.symbol));
176181
fmt
177182
}
178183
Ok(fmt) => fmt,
@@ -584,7 +589,12 @@ fn make_format_args(
584589
}
585590
}
586591

587-
ExpandResult::Ready(Ok(FormatArgs { span: fmt_span, template, arguments: args }))
592+
ExpandResult::Ready(Ok(FormatArgs {
593+
span: fmt_span,
594+
template,
595+
arguments: args,
596+
uncooked_fmt_str,
597+
}))
588598
}
589599

590600
fn invalid_placeholder_type_error(

Diff for: compiler/rustc_builtin_macros/src/util.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,17 @@ pub(crate) fn warn_on_duplicate_attribute(ecx: &ExtCtxt<'_>, item: &Annotatable,
5757

5858
/// `Ok` represents successfully retrieving the string literal at the correct
5959
/// position, e.g., `println("abc")`.
60-
type ExprToSpannedStringResult<'a> = Result<(Symbol, ast::StrStyle, Span), UnexpectedExprKind<'a>>;
60+
pub(crate) type ExprToSpannedStringResult<'a> = Result<ExprToSpannedString, UnexpectedExprKind<'a>>;
61+
62+
pub(crate) struct ExprToSpannedString {
63+
pub symbol: Symbol,
64+
pub style: ast::StrStyle,
65+
pub span: Span,
66+
/// The raw string literal, with no escaping or processing.
67+
///
68+
/// Generally only useful for lints that care about the raw bytes the user wrote.
69+
pub uncooked_symbol: (ast::token::LitKind, Symbol),
70+
}
6171

6272
/// - `Ok` is returned when the conversion to a string literal is unsuccessful,
6373
/// but another type of expression is obtained instead.
@@ -90,7 +100,12 @@ pub(crate) fn expr_to_spanned_string<'a>(
90100
ExpandResult::Ready(Err(match expr.kind {
91101
ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
92102
Ok(ast::LitKind::Str(s, style)) => {
93-
return ExpandResult::Ready(Ok((s, style, expr.span)));
103+
return ExpandResult::Ready(Ok(ExprToSpannedString {
104+
symbol: s,
105+
style,
106+
span: expr.span,
107+
uncooked_symbol: (token_lit.kind, token_lit.symbol),
108+
}));
94109
}
95110
Ok(ast::LitKind::ByteStr(..)) => {
96111
let mut err = cx.dcx().struct_span_err(expr.span, err_msg);
@@ -128,7 +143,7 @@ pub(crate) fn expr_to_string(
128143
Ok((err, _)) => err.emit(),
129144
Err(guar) => guar,
130145
})
131-
.map(|(symbol, style, _)| (symbol, style))
146+
.map(|ExprToSpannedString { symbol, style, .. }| (symbol, style))
132147
})
133148
}
134149

@@ -183,7 +198,7 @@ pub(crate) fn get_single_str_spanned_from_tts(
183198
Ok((err, _)) => err.emit(),
184199
Err(guar) => guar,
185200
})
186-
.map(|(symbol, _style, span)| (symbol, span))
201+
.map(|ExprToSpannedString { symbol, span, .. }| (symbol, span))
187202
})
188203
}
189204

Diff for: compiler/rustc_lint/src/hidden_unicode_codepoints.rs

+34-12
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,36 @@ impl HiddenUnicodeCodepoints {
8282
sub,
8383
});
8484
}
85+
86+
fn check_literal(
87+
&mut self,
88+
cx: &EarlyContext<'_>,
89+
text: Symbol,
90+
lit_kind: ast::token::LitKind,
91+
span: Span,
92+
label: &'static str,
93+
) {
94+
if !contains_text_flow_control_chars(text.as_str()) {
95+
return;
96+
}
97+
let (padding, point_at_inner_spans) = match lit_kind {
98+
// account for `"` or `'`
99+
ast::token::LitKind::Str | ast::token::LitKind::Char => (1, true),
100+
// account for `c"`
101+
ast::token::LitKind::CStr => (2, true),
102+
// account for `r###"`
103+
ast::token::LitKind::StrRaw(n) => (n as u32 + 2, true),
104+
// account for `cr###"`
105+
ast::token::LitKind::CStrRaw(n) => (n as u32 + 3, true),
106+
// suppress bad literals.
107+
ast::token::LitKind::Err(_) => return,
108+
// Be conservative just in case new literals do support these.
109+
_ => (0, false),
110+
};
111+
self.lint_text_direction_codepoint(cx, text, span, padding, point_at_inner_spans, label);
112+
}
85113
}
114+
86115
impl EarlyLintPass for HiddenUnicodeCodepoints {
87116
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
88117
if let ast::AttrKind::DocComment(_, comment) = attr.kind {
@@ -97,18 +126,11 @@ impl EarlyLintPass for HiddenUnicodeCodepoints {
97126
// byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString`
98127
match &expr.kind {
99128
ast::ExprKind::Lit(token_lit) => {
100-
let text = token_lit.symbol;
101-
if !contains_text_flow_control_chars(text.as_str()) {
102-
return;
103-
}
104-
let padding = match token_lit.kind {
105-
// account for `"` or `'`
106-
ast::token::LitKind::Str | ast::token::LitKind::Char => 1,
107-
// account for `r###"`
108-
ast::token::LitKind::StrRaw(n) => n as u32 + 2,
109-
_ => return,
110-
};
111-
self.lint_text_direction_codepoint(cx, text, expr.span, padding, true, "literal");
129+
self.check_literal(cx, token_lit.symbol, token_lit.kind, expr.span, "literal");
130+
}
131+
ast::ExprKind::FormatArgs(args) => {
132+
let (lit_kind, text) = args.uncooked_fmt_str;
133+
self.check_literal(cx, text, lit_kind, args.span, "format string");
112134
}
113135
_ => {}
114136
};

Diff for: library/core/src/ptr/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1403,8 +1403,6 @@ pub const unsafe fn read<T>(src: *const T) -> T {
14031403
/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the returned
14041404
/// value and the value at `*src` can [violate memory safety][read-ownership].
14051405
///
1406-
/// Note that even if `T` has size `0`, the pointer must be non-null.
1407-
///
14081406
/// [read-ownership]: read#ownership-of-the-returned-value
14091407
/// [valid]: self#safety
14101408
///
@@ -1611,8 +1609,6 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
16111609
///
16121610
/// * `dst` must be [valid] for writes.
16131611
///
1614-
/// Note that even if `T` has size `0`, the pointer must be non-null.
1615-
///
16161612
/// [valid]: self#safety
16171613
///
16181614
/// ## On `packed` structs

Diff for: src/doc/rustc/src/platform-support/arm64e-apple-darwin.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ You can build Rust with support for the targets by adding it to the `target` lis
2020

2121
```toml
2222
[build]
23-
target = [ "arm64e-apple-darwin" ]
23+
target = ["arm64e-apple-darwin"]
2424
```
2525

2626
## Building Rust programs

Diff for: src/doc/rustc/src/platform-support/arm64e-apple-ios.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ You can build Rust with support for the targets by adding it to the `target` lis
1818

1919
```toml
2020
[build]
21-
target = [ "arm64e-apple-ios" ]
21+
target = ["arm64e-apple-ios"]
2222
```
2323

2424
## Building Rust programs

Diff for: src/doc/rustc/src/platform-support/arm64e-apple-tvos.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ You can build Rust with support for the targets by adding it to the `target` lis
1919

2020
```toml
2121
[build]
22-
target = [ "arm64e-apple-tvos" ]
22+
target = ["arm64e-apple-tvos"]
2323
```
2424

2525
## Building Rust programs

Diff for: src/doc/rustc/src/platform-support/arm64ec-pc-windows-msvc.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ list in `config.toml`:
6060

6161
```toml
6262
[build]
63-
target = [ "arm64ec-pc-windows-msvc" ]
63+
target = ["arm64ec-pc-windows-msvc"]
6464
```
6565

6666
## Building Rust programs

Diff for: src/doc/rustc/src/platform-support/hexagon-unknown-linux-musl.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ target list in `config.toml`, a sample configuration is shown below.
4848

4949
```toml
5050
[build]
51-
target = [ "hexagon-unknown-linux-musl"]
51+
target = ["hexagon-unknown-linux-musl"]
5252

5353
[target.hexagon-unknown-linux-musl]
5454

Diff for: src/doc/rustc/src/platform-support/unikraft-linux-musl.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ You can build Rust with support for the targets by adding it to the `target` lis
3939
```toml
4040
[build]
4141
build-stage = 1
42-
target = [ "x86_64-unikraft-linux-musl" ]
42+
target = ["x86_64-unikraft-linux-musl"]
4343
```
4444

4545
## Building Rust programs

Diff for: src/doc/rustc/src/platform-support/win7-windows-msvc.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ You can build Rust with support for the targets by adding it to the target list
2525
```toml
2626
[build]
2727
build-stage = 1
28-
target = [ "x86_64-win7-windows-msvc" ]
28+
target = ["x86_64-win7-windows-msvc"]
2929
```
3030

3131
## Building Rust programs

Diff for: src/doc/rustc/src/target-tier-policy.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ To propose addition of a new target, open a pull request on [`rust-lang/rust`]:
119119
Link to the created description page.
120120
- Ensure the pull request is assigned to a member of the [Rust compiler team][rust_compiler_team] by commenting:
121121
```text
122-
r? compiler-team
122+
r? compiler
123123
```
124124

125125
[tier3example]: https://github.com/rust-lang/rust/pull/94872

Diff for: src/tools/tidy/src/issues.txt

-2
Original file line numberDiff line numberDiff line change
@@ -2710,8 +2710,6 @@ ui/limits/issue-75158-64.rs
27102710
ui/link-native-libs/issue-109144.rs
27112711
ui/link-native-libs/issue-43925.rs
27122712
ui/link-native-libs/issue-43926.rs
2713-
ui/link-native-libs/issue-70093/issue-70093-link-directives.rs
2714-
ui/link-native-libs/issue-70093/issue-70093.rs
27152713
ui/linkage-attr/auxiliary/issue-12133-dylib.rs
27162714
ui/linkage-attr/auxiliary/issue-12133-dylib2.rs
27172715
ui/linkage-attr/auxiliary/issue-12133-rlib.rs

Diff for: tests/ui/link-native-libs/issue-70093/issue-70093-link-directives.rs

-10
This file was deleted.

Diff for: tests/ui/link-native-libs/issue-70093/issue-70093.rs

-10
This file was deleted.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Ensure that `#[link]` attributes are entirely ignore when using `-Zlink-directives=no`.
2+
3+
//@ run-pass
4+
//@ compile-flags: -Zlink-directives=no
5+
//@ ignore-fuchsia - missing __libc_start_main for some reason (#84733)
6+
//@ ignore-cross-compile - default-linker-libraries=yes doesn't play well with cross compiling
7+
8+
// Usually these `#[link]` attribute would cause `libsome-random-non-existent-library`
9+
// to be passed to the linker, causing it to fail because the file doesn't exist.
10+
// However, with -Zlink-directives=no, the `#[link]` is ignored.
11+
#[link(name = "some-random-non-existent-library", kind = "static")]
12+
extern "C" {}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Ensure that rust does not pass native libraries to the linker when
2+
// `-Zlink-native-libraries=no` is used.
3+
4+
//@ run-pass
5+
//@ compile-flags: -Zlink-native-libraries=no -Cdefault-linker-libraries=yes
6+
//@ ignore-fuchsia - missing __libc_start_main for some reason (#84733)
7+
//@ ignore-cross-compile - default-linker-libraries=yes doesn't play well with cross compiling
8+
9+
//@ revisions: other
10+
//@[other] ignore-msvc
11+
12+
//@ revisions: msvc
13+
// On Windows MSVC, default-linker-libraries=yes doesn't work because
14+
// rustc drives the linker directly instead of going through another compiler.
15+
// Therefore rustc would need to implement default-linker-libraries itself but doesn't.
16+
// So instead we use -Clink-arg to directly set the required msvcrt.lib as a link arg.
17+
//@[msvc] compile-flags: -Clink-arg=msvcrt.lib
18+
//@[msvc] only-msvc
19+
20+
// Usually these `#[link]` attribute would cause `libsome-random-non-existent-library`
21+
// to be passed to the linker, causing it to fail because the file doesn't exist.
22+
// However, -Zlink-native-libraries=no disables that.
23+
#[link(name = "some-random-non-existent-library", kind = "static")]
24+
extern "C" {}
25+
26+
fn main() {}

Diff for: tests/ui/parser/unicode-control-codepoints.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ edition: 2021
2+
13
fn main() {
24
// if access_level != "us‫e‪r" { // Check if admin
35
//~^ ERROR unicode codepoint changing visible direction of text present in comment
@@ -25,6 +27,14 @@ fn main() {
2527
//~| ERROR non-ASCII character in raw byte string literal
2628
println!("{:?}", '‮');
2729
//~^ ERROR unicode codepoint changing visible direction of text present in literal
30+
31+
let _ = c"‮";
32+
//~^ ERROR unicode codepoint changing visible direction of text present in literal
33+
let _ = cr#"‮"#;
34+
//~^ ERROR unicode codepoint changing visible direction of text present in literal
35+
36+
println!("{{‮}}");
37+
//~^ ERROR unicode codepoint changing visible direction of text present in format string
2838
}
2939

3040
//"/*‮ } ⁦if isAdmin⁩ ⁦ begin admins only */"

0 commit comments

Comments
 (0)