Skip to content

Commit 95f6a01

Browse files
committed
Auto merge of rust-lang#117272 - matthiaskrgr:rollup-upg122z, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - rust-lang#114998 (feat(docs): add cargo-pgo to PGO documentation 📝) - rust-lang#116868 (Tweak suggestion span for outer attr and point at item following invalid inner attr) - rust-lang#117240 (Fix documentation typo in std::iter::Iterator::collect_into) - rust-lang#117241 (Stash and cancel cycle errors for auto trait leakage in opaques) - rust-lang#117262 (Create a new ConstantKind variant (ZeroSized) for StableMIR) - rust-lang#117266 (replace transmute by raw pointer cast) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 54e57e6 + 2fdac63 commit 95f6a01

File tree

32 files changed

+265
-309
lines changed

32 files changed

+265
-309
lines changed

compiler/rustc_errors/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ pub enum StashKey {
508508
TraitMissingMethod,
509509
OpaqueHiddenTypeMismatch,
510510
MaybeForgetReturn,
511+
/// Query cycle detected, stashing in favor of a better error.
512+
Cycle,
511513
}
512514

513515
fn default_track_diagnostic(d: &mut Diagnostic, f: &mut dyn FnMut(&mut Diagnostic)) {

compiler/rustc_macros/src/query.rs

+8
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ struct QueryModifiers {
9797
/// A cycle error results in a delay_bug call
9898
cycle_delay_bug: Option<Ident>,
9999

100+
/// A cycle error results in a stashed cycle error that can be unstashed and canceled later
101+
cycle_stash: Option<Ident>,
102+
100103
/// Don't hash the result, instead just mark a query red if it runs
101104
no_hash: Option<Ident>,
102105

@@ -127,6 +130,7 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
127130
let mut desc = None;
128131
let mut fatal_cycle = None;
129132
let mut cycle_delay_bug = None;
133+
let mut cycle_stash = None;
130134
let mut no_hash = None;
131135
let mut anon = None;
132136
let mut eval_always = None;
@@ -181,6 +185,8 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
181185
try_insert!(fatal_cycle = modifier);
182186
} else if modifier == "cycle_delay_bug" {
183187
try_insert!(cycle_delay_bug = modifier);
188+
} else if modifier == "cycle_stash" {
189+
try_insert!(cycle_stash = modifier);
184190
} else if modifier == "no_hash" {
185191
try_insert!(no_hash = modifier);
186192
} else if modifier == "anon" {
@@ -208,6 +214,7 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
208214
desc,
209215
fatal_cycle,
210216
cycle_delay_bug,
217+
cycle_stash,
211218
no_hash,
212219
anon,
213220
eval_always,
@@ -329,6 +336,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
329336
fatal_cycle,
330337
arena_cache,
331338
cycle_delay_bug,
339+
cycle_stash,
332340
no_hash,
333341
anon,
334342
eval_always,

compiler/rustc_middle/src/query/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ rustc_queries! {
251251
"computing type of opaque `{path}`",
252252
path = tcx.def_path_str(key),
253253
}
254+
cycle_stash
254255
}
255256

256257
query type_alias_is_lazy(key: DefId) -> bool {

compiler/rustc_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ passes_invalid_attr_at_crate_level =
393393
`{$name}` attribute cannot be used at crate level
394394
.suggestion = perhaps you meant to use an outer attribute
395395
396+
passes_invalid_attr_at_crate_level_item =
397+
the inner attribute doesn't annotate this {$kind}
398+
396399
passes_invalid_deprecation_version =
397400
invalid deprecation version found
398401
.label = invalid deprecation version

compiler/rustc_passes/src/check_attr.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -2534,10 +2534,30 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
25342534
if attr.style == AttrStyle::Inner {
25352535
for attr_to_check in ATTRS_TO_CHECK {
25362536
if attr.has_name(*attr_to_check) {
2537+
let item = tcx
2538+
.hir()
2539+
.items()
2540+
.map(|id| tcx.hir().item(id))
2541+
.find(|item| !item.span.is_dummy()) // Skip prelude `use`s
2542+
.map(|item| errors::ItemFollowingInnerAttr {
2543+
span: item.ident.span,
2544+
kind: item.kind.descr(),
2545+
});
25372546
tcx.sess.emit_err(errors::InvalidAttrAtCrateLevel {
25382547
span: attr.span,
2539-
snippet: tcx.sess.source_map().span_to_snippet(attr.span).ok(),
2548+
sugg_span: tcx
2549+
.sess
2550+
.source_map()
2551+
.span_to_snippet(attr.span)
2552+
.ok()
2553+
.filter(|src| src.starts_with("#!["))
2554+
.map(|_| {
2555+
attr.span
2556+
.with_lo(attr.span.lo() + BytePos(1))
2557+
.with_hi(attr.span.lo() + BytePos(2))
2558+
}),
25402559
name: *attr_to_check,
2560+
item,
25412561
});
25422562
}
25432563
}

compiler/rustc_passes/src/errors.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,15 @@ pub struct UnknownLangItem {
856856

857857
pub struct InvalidAttrAtCrateLevel {
858858
pub span: Span,
859-
pub snippet: Option<String>,
859+
pub sugg_span: Option<Span>,
860860
pub name: Symbol,
861+
pub item: Option<ItemFollowingInnerAttr>,
862+
}
863+
864+
#[derive(Clone, Copy)]
865+
pub struct ItemFollowingInnerAttr {
866+
pub span: Span,
867+
pub kind: &'static str,
861868
}
862869

863870
impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
@@ -871,15 +878,18 @@ impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
871878
diag.set_arg("name", self.name);
872879
// Only emit an error with a suggestion if we can create a string out
873880
// of the attribute span
874-
if let Some(src) = self.snippet {
875-
let replacement = src.replace("#!", "#");
881+
if let Some(span) = self.sugg_span {
876882
diag.span_suggestion_verbose(
877-
self.span,
883+
span,
878884
fluent::passes_suggestion,
879-
replacement,
885+
String::new(),
880886
rustc_errors::Applicability::MachineApplicable,
881887
);
882888
}
889+
if let Some(item) = self.item {
890+
diag.set_arg("kind", item.kind);
891+
diag.span_label(item.span, fluent::passes_invalid_attr_at_crate_level_item);
892+
}
883893
diag
884894
}
885895
}

compiler/rustc_query_impl/src/plumbing.rs

+3
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ macro_rules! handle_cycle_error {
197197
([(fatal_cycle) $($rest:tt)*]) => {{
198198
rustc_query_system::HandleCycleError::Fatal
199199
}};
200+
([(cycle_stash) $($rest:tt)*]) => {{
201+
rustc_query_system::HandleCycleError::Stash
202+
}};
200203
([(cycle_delay_bug) $($rest:tt)*]) => {{
201204
rustc_query_system::HandleCycleError::DelayBug
202205
}};

compiler/rustc_query_system/src/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub enum HandleCycleError {
1515
Error,
1616
Fatal,
1717
DelayBug,
18+
Stash,
1819
}
1920

2021
#[derive(Subdiagnostic)]

compiler/rustc_query_system/src/query/plumbing.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
1919
use rustc_data_structures::sync::Lock;
2020
#[cfg(parallel_compiler)]
2121
use rustc_data_structures::{outline, sync};
22-
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, FatalError};
22+
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, FatalError, StashKey};
2323
use rustc_span::{Span, DUMMY_SP};
2424
use std::cell::Cell;
2525
use std::collections::hash_map::Entry;
@@ -133,6 +133,17 @@ where
133133
let guar = error.delay_as_bug();
134134
query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle, guar)
135135
}
136+
Stash => {
137+
let guar = if let Some(root) = cycle_error.cycle.first()
138+
&& let Some(span) = root.query.span
139+
{
140+
error.stash(span, StashKey::Cycle);
141+
qcx.dep_context().sess().delay_span_bug(span, "delayed cycle error")
142+
} else {
143+
error.emit()
144+
};
145+
query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle, guar)
146+
}
136147
}
137148
}
138149

compiler/rustc_smir/src/rustc_smir/mod.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1283,11 +1283,15 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
12831283
let kind = match self.kind() {
12841284
ty::Value(val) => {
12851285
let const_val = tables.tcx.valtree_to_const_val((self.ty(), val));
1286-
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
1287-
self.ty(),
1288-
const_val,
1289-
tables,
1290-
))
1286+
if matches!(const_val, mir::ConstValue::ZeroSized) {
1287+
ConstantKind::ZeroSized
1288+
} else {
1289+
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
1290+
self.ty(),
1291+
const_val,
1292+
tables,
1293+
))
1294+
}
12911295
}
12921296
ty::ParamCt(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)),
12931297
ty::ErrorCt(_) => unreachable!(),
@@ -1401,6 +1405,11 @@ impl<'tcx> Stable<'tcx> for rustc_middle::mir::Const<'tcx> {
14011405
let id = tables.intern_const(*self);
14021406
Const::new(kind, ty, id)
14031407
}
1408+
mir::Const::Val(val, ty) if matches!(val, mir::ConstValue::ZeroSized) => {
1409+
let ty = ty.stable(tables);
1410+
let id = tables.intern_const(*self);
1411+
Const::new(ConstantKind::ZeroSized, ty, id)
1412+
}
14041413
mir::Const::Val(val, ty) => {
14051414
let kind = ConstantKind::Allocated(alloc::new_allocation(ty, val, tables));
14061415
let ty = ty.stable(tables);

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3104,6 +3104,13 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
31043104
);
31053105
}
31063106
};
3107+
3108+
if let Some(diag) =
3109+
self.tcx.sess.diagnostic().steal_diagnostic(self.tcx.def_span(def_id), StashKey::Cycle)
3110+
{
3111+
diag.cancel();
3112+
}
3113+
31073114
err
31083115
}
31093116

compiler/stable_mir/src/ty.rs

+3
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ pub enum ConstantKind {
444444
Allocated(Allocation),
445445
Unevaluated(UnevaluatedConst),
446446
Param(ParamConst),
447+
/// Store ZST constants.
448+
/// We have to special handle these constants since its type might be generic.
449+
ZeroSized,
447450
}
448451

449452
#[derive(Clone, Debug)]

compiler/stable_mir/src/visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Visitable for Const {
5050
match &self.kind() {
5151
super::ty::ConstantKind::Allocated(alloc) => alloc.visit(visitor)?,
5252
super::ty::ConstantKind::Unevaluated(uv) => uv.visit(visitor)?,
53-
super::ty::ConstantKind::Param(_) => {}
53+
super::ty::ConstantKind::Param(_) | super::ty::ConstantKind::ZeroSized => {}
5454
}
5555
self.ty().visit(visitor)
5656
}

library/core/src/iter/traits/iterator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ pub trait Iterator {
21422142
/// passed collection. The collection is then returned, so the call chain
21432143
/// can be continued.
21442144
///
2145-
/// This is useful when you already have a collection and wants to add
2145+
/// This is useful when you already have a collection and want to add
21462146
/// the iterator items to it.
21472147
///
21482148
/// This method is a convenience method to call [Extend::extend](trait.Extend.html),

library/std/src/thread/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,13 @@ impl Builder {
545545
scope_data.increment_num_running_threads();
546546
}
547547

548+
let main = Box::new(main);
549+
#[cfg(bootstrap)]
550+
let main =
551+
unsafe { mem::transmute::<Box<dyn FnOnce() + 'a>, Box<dyn FnOnce() + 'static>>(main) };
552+
#[cfg(not(bootstrap))]
553+
let main = unsafe { Box::from_raw(Box::into_raw(main) as *mut (dyn FnOnce() + 'static)) };
554+
548555
Ok(JoinInner {
549556
// SAFETY:
550557
//
@@ -559,14 +566,7 @@ impl Builder {
559566
// Similarly, the `sys` implementation must guarantee that no references to the closure
560567
// exist after the thread has terminated, which is signaled by `Thread::join`
561568
// returning.
562-
native: unsafe {
563-
imp::Thread::new(
564-
stack_size,
565-
mem::transmute::<Box<dyn FnOnce() + 'a>, Box<dyn FnOnce() + 'static>>(
566-
Box::new(main),
567-
),
568-
)?
569-
},
569+
native: unsafe { imp::Thread::new(stack_size, main)? },
570570
thread: my_thread,
571571
packet: my_packet,
572572
})

src/doc/rustc/src/profile-guided-optimization.md

+23
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,26 @@ in Clang's documentation is therefore an interesting read for anyone who wants
145145
to use PGO with Rust.
146146

147147
[clang-pgo]: https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
148+
149+
## Community Maintained Tools
150+
151+
As an alternative to directly using the compiler for Profile-Guided Optimization,
152+
you may choose to go with `cargo-pgo`, which has an intuitive command-line API
153+
and saves you the trouble of doing all the manual work. You can read more about
154+
it in their repository accessible from this link: https://github.com/Kobzol/cargo-pgo
155+
156+
For the sake of completeness, here are the corresponding steps using `cargo-pgo`:
157+
158+
```bash
159+
# Install if you haven't already
160+
cargo install cargo-pgo
161+
162+
cargo pgo build
163+
cargo pgo optimize
164+
```
165+
166+
These steps will do the following just as before:
167+
168+
1. Build an instrumented binary from the source code.
169+
2. Run the instrumented binary to gather PGO profiles.
170+
3. Use the gathered PGO profiles from the last step to build an optimized binary.

tests/ui/attributes/unix_sigpipe/unix_sigpipe-crate.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ error: `unix_sigpipe` attribute cannot be used at crate level
33
|
44
LL | #![unix_sigpipe = "inherit"]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
LL |
7+
LL | fn main() {}
8+
| ---- the inner attribute doesn't annotate this function
69
|
710
help: perhaps you meant to use an outer attribute
811
|
9-
LL | #[unix_sigpipe = "inherit"]
10-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
LL - #![unix_sigpipe = "inherit"]
13+
LL + #[unix_sigpipe = "inherit"]
14+
|
1115

1216
error: aborting due to previous error
1317

0 commit comments

Comments
 (0)