Skip to content

Commit b6057bf

Browse files
committed
Auto merge of rust-lang#89435 - Manishearth:rollup-vh2ih7k, r=Manishearth
Rollup of 6 pull requests Successful merges: - rust-lang#87868 (Added -Z randomize-layout flag) - rust-lang#88820 (Add `pie` as another `relocation-model` value) - rust-lang#89029 (feat(rustc_parse): recover from pre-RFC-2000 const generics syntax) - rust-lang#89322 (Reapply "Remove optimization_fuel_crate from Session") - rust-lang#89340 (Improve error message for `printf`-style format strings) - rust-lang#89415 (Correct caller/callsite confusion in inliner message) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents ed93759 + 534946c commit b6057bf

File tree

36 files changed

+527
-66
lines changed

36 files changed

+527
-66
lines changed

Cargo.lock

+16-5
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ checksum = "3ca8957e71f04a205cb162508f9326aea04676c8dfd0711220190d6b83664f3f"
16601660
dependencies = [
16611661
"bitmaps",
16621662
"rand_core 0.5.1",
1663-
"rand_xoshiro",
1663+
"rand_xoshiro 0.4.0",
16641664
"sized-chunks",
16651665
"typenum",
16661666
"version_check",
@@ -2256,7 +2256,7 @@ dependencies = [
22562256
"libc",
22572257
"log",
22582258
"measureme",
2259-
"rand 0.8.3",
2259+
"rand 0.8.4",
22602260
"rustc-workspace-hack",
22612261
"rustc_version",
22622262
"shell-escape",
@@ -2852,9 +2852,9 @@ dependencies = [
28522852

28532853
[[package]]
28542854
name = "rand"
2855-
version = "0.8.3"
2855+
version = "0.8.4"
28562856
source = "registry+https://github.com/rust-lang/crates.io-index"
2857-
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
2857+
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
28582858
dependencies = [
28592859
"libc",
28602860
"rand_chacha 0.3.0",
@@ -2945,6 +2945,15 @@ dependencies = [
29452945
"rand_core 0.5.1",
29462946
]
29472947

2948+
[[package]]
2949+
name = "rand_xoshiro"
2950+
version = "0.6.0"
2951+
source = "registry+https://github.com/rust-lang/crates.io-index"
2952+
checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa"
2953+
dependencies = [
2954+
"rand_core 0.6.2",
2955+
]
2956+
29482957
[[package]]
29492958
name = "rayon"
29502959
version = "1.3.1"
@@ -4087,6 +4096,8 @@ dependencies = [
40874096
"either",
40884097
"gsgdt",
40894098
"polonius-engine",
4099+
"rand 0.8.4",
4100+
"rand_xoshiro 0.6.0",
40904101
"rustc-rayon-core",
40914102
"rustc_apfloat",
40924103
"rustc_arena",
@@ -5097,7 +5108,7 @@ checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"
50975108
dependencies = [
50985109
"cfg-if 1.0.0",
50995110
"libc",
5100-
"rand 0.8.3",
5111+
"rand 0.8.4",
51015112
"redox_syscall",
51025113
"remove_dir_all",
51035114
"winapi",

compiler/rustc_builtin_macros/src/format.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -1154,11 +1154,12 @@ pub fn expand_preparsed_format_args(
11541154
// account for `"` and account for raw strings `r#`
11551155
let padding = str_style.map(|i| i + 2).unwrap_or(1);
11561156
for sub in foreign::$kind::iter_subs(fmt_str, padding) {
1157-
let trn = match sub.translate() {
1158-
Some(trn) => trn,
1157+
let (trn, success) = match sub.translate() {
1158+
Ok(trn) => (trn, true),
1159+
Err(Some(msg)) => (msg, false),
11591160

11601161
// If it has no translation, don't call it out specifically.
1161-
None => continue,
1162+
_ => continue,
11621163
};
11631164

11641165
let pos = sub.position();
@@ -1175,9 +1176,24 @@ pub fn expand_preparsed_format_args(
11751176

11761177
if let Some(inner_sp) = pos {
11771178
let sp = fmt_sp.from_inner(inner_sp);
1178-
suggestions.push((sp, trn));
1179+
1180+
if success {
1181+
suggestions.push((sp, trn));
1182+
} else {
1183+
diag.span_note(
1184+
sp,
1185+
&format!("format specifiers use curly braces, and {}", trn),
1186+
);
1187+
}
11791188
} else {
1180-
diag.help(&format!("`{}` should be written as `{}`", sub, trn));
1189+
if success {
1190+
diag.help(&format!("`{}` should be written as `{}`", sub, trn));
1191+
} else {
1192+
diag.note(&format!(
1193+
"`{}` should use curly braces, and {}",
1194+
sub, trn
1195+
));
1196+
}
11811197
}
11821198
}
11831199

compiler/rustc_builtin_macros/src/format_foreign.rs

+48-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub mod printf {
1+
pub(crate) mod printf {
22
use super::strcursor::StrCursor as Cur;
33
use rustc_span::InnerSpan;
44

@@ -36,10 +36,10 @@ pub mod printf {
3636
///
3737
/// This ignores cases where the substitution does not have an exact equivalent, or where
3838
/// the substitution would be unnecessary.
39-
pub fn translate(&self) -> Option<String> {
39+
pub fn translate(&self) -> Result<String, Option<String>> {
4040
match *self {
4141
Substitution::Format(ref fmt) => fmt.translate(),
42-
Substitution::Escape => None,
42+
Substitution::Escape => Err(None),
4343
}
4444
}
4545
}
@@ -68,9 +68,9 @@ pub mod printf {
6868
impl Format<'_> {
6969
/// Translate this directive into an equivalent Rust formatting directive.
7070
///
71-
/// Returns `None` in cases where the `printf` directive does not have an exact Rust
71+
/// Returns `Err` in cases where the `printf` directive does not have an exact Rust
7272
/// equivalent, rather than guessing.
73-
pub fn translate(&self) -> Option<String> {
73+
pub fn translate(&self) -> Result<String, Option<String>> {
7474
use std::fmt::Write;
7575

7676
let (c_alt, c_zero, c_left, c_plus) = {
@@ -84,7 +84,12 @@ pub mod printf {
8484
'0' => c_zero = true,
8585
'-' => c_left = true,
8686
'+' => c_plus = true,
87-
_ => return None,
87+
_ => {
88+
return Err(Some(format!(
89+
"the flag `{}` is unknown or unsupported",
90+
c
91+
)));
92+
}
8893
}
8994
}
9095
(c_alt, c_zero, c_left, c_plus)
@@ -104,7 +109,9 @@ pub mod printf {
104109
let width = match self.width {
105110
Some(Num::Next) => {
106111
// NOTE: Rust doesn't support this.
107-
return None;
112+
return Err(Some(
113+
"you have to use a positional or named parameter for the width".to_string(),
114+
));
108115
}
109116
w @ Some(Num::Arg(_)) => w,
110117
w @ Some(Num::Num(_)) => w,
@@ -125,13 +132,21 @@ pub mod printf {
125132
"p" => (Some(self.type_), false, true),
126133
"g" => (Some("e"), true, false),
127134
"G" => (Some("E"), true, false),
128-
_ => return None,
135+
_ => {
136+
return Err(Some(format!(
137+
"the conversion specifier `{}` is unknown or unsupported",
138+
self.type_
139+
)));
140+
}
129141
};
130142

131143
let (fill, width, precision) = match (is_int, width, precision) {
132144
(true, Some(_), Some(_)) => {
133145
// Rust can't duplicate this insanity.
134-
return None;
146+
return Err(Some(
147+
"width and precision cannot both be specified for integer conversions"
148+
.to_string(),
149+
));
135150
}
136151
(true, None, Some(p)) => (Some("0"), Some(p), None),
137152
(true, w, None) => (fill, w, None),
@@ -169,7 +184,17 @@ pub mod printf {
169184
s.push('{');
170185

171186
if let Some(arg) = self.parameter {
172-
write!(s, "{}", arg.checked_sub(1)?).ok()?;
187+
match write!(
188+
s,
189+
"{}",
190+
match arg.checked_sub(1) {
191+
Some(a) => a,
192+
None => return Err(None),
193+
}
194+
) {
195+
Err(_) => return Err(None),
196+
_ => {}
197+
}
173198
}
174199

175200
if has_options {
@@ -199,12 +224,18 @@ pub mod printf {
199224
}
200225

201226
if let Some(width) = width {
202-
width.translate(&mut s).ok()?;
227+
match width.translate(&mut s) {
228+
Err(_) => return Err(None),
229+
_ => {}
230+
}
203231
}
204232

205233
if let Some(precision) = precision {
206234
s.push('.');
207-
precision.translate(&mut s).ok()?;
235+
match precision.translate(&mut s) {
236+
Err(_) => return Err(None),
237+
_ => {}
238+
}
208239
}
209240

210241
if let Some(type_) = type_ {
@@ -213,7 +244,7 @@ pub mod printf {
213244
}
214245

215246
s.push('}');
216-
Some(s)
247+
Ok(s)
217248
}
218249
}
219250

@@ -623,11 +654,11 @@ pub mod shell {
623654
}
624655
}
625656

626-
pub fn translate(&self) -> Option<String> {
657+
pub fn translate(&self) -> Result<String, Option<String>> {
627658
match *self {
628-
Substitution::Ordinal(n, _) => Some(format!("{{{}}}", n)),
629-
Substitution::Name(n, _) => Some(format!("{{{}}}", n)),
630-
Substitution::Escape(_) => None,
659+
Substitution::Ordinal(n, _) => Ok(format!("{{{}}}", n)),
660+
Substitution::Name(n, _) => Ok(format!("{{{}}}", n)),
661+
Substitution::Escape(_) => Err(None),
631662
}
632663
}
633664
}

compiler/rustc_builtin_macros/src/format_foreign/printf/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::{iter_subs, parse_next_substitution as pns, Format as F, Num as N, Su
33
macro_rules! assert_eq_pnsat {
44
($lhs:expr, $rhs:expr) => {
55
assert_eq!(
6-
pns($lhs).and_then(|(s, _)| s.translate()),
6+
pns($lhs).and_then(|(s, _)| s.translate().ok()),
77
$rhs.map(<String as From<&str>>::from)
88
)
99
};
@@ -98,7 +98,7 @@ fn test_parse() {
9898
#[test]
9999
fn test_iter() {
100100
let s = "The %d'th word %% is: `%.*s` %!\n";
101-
let subs: Vec<_> = iter_subs(s, 0).map(|sub| sub.translate()).collect();
101+
let subs: Vec<_> = iter_subs(s, 0).map(|sub| sub.translate().ok()).collect();
102102
assert_eq!(
103103
subs.iter().map(|ms| ms.as_ref().map(|s| &s[..])).collect::<Vec<_>>(),
104104
vec![Some("{}"), None, Some("{:.*}"), None]

compiler/rustc_builtin_macros/src/format_foreign/shell/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::{parse_next_substitution as pns, Substitution as S};
33
macro_rules! assert_eq_pnsat {
44
($lhs:expr, $rhs:expr) => {
55
assert_eq!(
6-
pns($lhs).and_then(|(f, _)| f.translate()),
6+
pns($lhs).and_then(|(f, _)| f.translate().ok()),
77
$rhs.map(<String as From<&str>>::from)
88
)
99
};
@@ -37,7 +37,7 @@ fn test_parse() {
3737
fn test_iter() {
3838
use super::iter_subs;
3939
let s = "The $0'th word $$ is: `$WORD` $!\n";
40-
let subs: Vec<_> = iter_subs(s, 0).map(|sub| sub.translate()).collect();
40+
let subs: Vec<_> = iter_subs(s, 0).map(|sub| sub.translate().ok()).collect();
4141
assert_eq!(
4242
subs.iter().map(|ms| ms.as_ref().map(|s| &s[..])).collect::<Vec<_>>(),
4343
vec![Some("{0}"), None, Some("{WORD}")]

compiler/rustc_codegen_llvm/src/back/write.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ fn to_pass_builder_opt_level(cfg: config::OptLevel) -> llvm::PassBuilderOptLevel
129129
fn to_llvm_relocation_model(relocation_model: RelocModel) -> llvm::RelocModel {
130130
match relocation_model {
131131
RelocModel::Static => llvm::RelocModel::Static,
132-
RelocModel::Pic => llvm::RelocModel::PIC,
132+
// LLVM doesn't have a PIE relocation model, it represents PIE as PIC with an extra attribute.
133+
RelocModel::Pic | RelocModel::Pie => llvm::RelocModel::PIC,
133134
RelocModel::DynamicNoPic => llvm::RelocModel::DynamicNoPic,
134135
RelocModel::Ropi => llvm::RelocModel::ROPI,
135136
RelocModel::Rwpi => llvm::RelocModel::RWPI,

compiler/rustc_codegen_llvm/src/context.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,14 @@ pub unsafe fn create_module(
195195
let llvm_target = SmallCStr::new(&sess.target.llvm_target);
196196
llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
197197

198-
if sess.relocation_model() == RelocModel::Pic {
198+
let reloc_model = sess.relocation_model();
199+
if matches!(reloc_model, RelocModel::Pic | RelocModel::Pie) {
199200
llvm::LLVMRustSetModulePICLevel(llmod);
200201
// PIE is potentially more effective than PIC, but can only be used in executables.
201202
// If all our outputs are executables, then we can relax PIC to PIE.
202-
if sess.crate_types().iter().all(|ty| *ty == CrateType::Executable) {
203+
if reloc_model == RelocModel::Pie
204+
|| sess.crate_types().iter().all(|ty| *ty == CrateType::Executable)
205+
{
203206
llvm::LLVMRustSetModulePIELevel(llmod);
204207
}
205208
}

compiler/rustc_codegen_llvm/src/lib.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,16 @@ impl CodegenBackend for LlvmCodegenBackend {
211211
match req {
212212
PrintRequest::RelocationModels => {
213213
println!("Available relocation models:");
214-
for name in
215-
&["static", "pic", "dynamic-no-pic", "ropi", "rwpi", "ropi-rwpi", "default"]
216-
{
214+
for name in &[
215+
"static",
216+
"pic",
217+
"pie",
218+
"dynamic-no-pic",
219+
"ropi",
220+
"rwpi",
221+
"ropi-rwpi",
222+
"default",
223+
] {
217224
println!(" {}", name);
218225
}
219226
println!();

compiler/rustc_codegen_llvm/src/mono_item.rs

+6
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ impl CodegenCx<'ll, 'tcx> {
143143
return true;
144144
}
145145

146+
// With pie relocation model calls of functions defined in the translation
147+
// unit can use copy relocations.
148+
if self.tcx.sess.relocation_model() == RelocModel::Pie && !is_declaration {
149+
return true;
150+
}
151+
146152
return false;
147153
}
148154
}

compiler/rustc_codegen_ssa/src/back/link.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1490,9 +1490,13 @@ fn exec_linker(
14901490
fn link_output_kind(sess: &Session, crate_type: CrateType) -> LinkOutputKind {
14911491
let kind = match (crate_type, sess.crt_static(Some(crate_type)), sess.relocation_model()) {
14921492
(CrateType::Executable, _, _) if sess.is_wasi_reactor() => LinkOutputKind::WasiReactorExe,
1493-
(CrateType::Executable, false, RelocModel::Pic) => LinkOutputKind::DynamicPicExe,
1493+
(CrateType::Executable, false, RelocModel::Pic | RelocModel::Pie) => {
1494+
LinkOutputKind::DynamicPicExe
1495+
}
14941496
(CrateType::Executable, false, _) => LinkOutputKind::DynamicNoPicExe,
1495-
(CrateType::Executable, true, RelocModel::Pic) => LinkOutputKind::StaticPicExe,
1497+
(CrateType::Executable, true, RelocModel::Pic | RelocModel::Pie) => {
1498+
LinkOutputKind::StaticPicExe
1499+
}
14961500
(CrateType::Executable, true, _) => LinkOutputKind::StaticNoPicExe,
14971501
(_, true, _) => LinkOutputKind::StaticDylib,
14981502
(_, false, _) => LinkOutputKind::DynamicDylib,

compiler/rustc_hir/src/hir.rs

+10
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,16 @@ impl GenericArgs<'_> {
384384
self.args.iter().any(|arg| matches!(arg, GenericArg::Type(_)))
385385
}
386386

387+
pub fn has_err(&self) -> bool {
388+
self.args.iter().any(|arg| match arg {
389+
GenericArg::Type(ty) => matches!(ty.kind, TyKind::Err),
390+
_ => false,
391+
}) || self.bindings.iter().any(|arg| match arg.kind {
392+
TypeBindingKind::Equality { ty } => matches!(ty.kind, TyKind::Err),
393+
_ => false,
394+
})
395+
}
396+
387397
#[inline]
388398
pub fn num_type_params(&self) -> usize {
389399
self.args.iter().filter(|arg| matches!(arg, GenericArg::Type(_))).count()

compiler/rustc_middle/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ chalk-ir = "0.55.0"
3232
smallvec = { version = "1.6.1", features = ["union", "may_dangle"] }
3333
rustc_session = { path = "../rustc_session" }
3434
rustc_type_ir = { path = "../rustc_type_ir" }
35+
rand = "0.8.4"
36+
rand_xoshiro = "0.6.0"

0 commit comments

Comments
 (0)