Skip to content

Commit

Permalink
Auto merge of #117272 - matthiaskrgr:rollup-upg122z, r=matthiaskrgr
Browse files Browse the repository at this point in the history
Rollup of 6 pull requests

Successful merges:

 - #114998 (feat(docs): add cargo-pgo to PGO documentation 📝)
 - #116868 (Tweak suggestion span for outer attr and point at item following invalid inner attr)
 - #117240 (Fix documentation typo in std::iter::Iterator::collect_into)
 - #117241 (Stash and cancel cycle errors for auto trait leakage in opaques)
 - #117262 (Create a new ConstantKind variant (ZeroSized) for StableMIR)
 - #117266 (replace transmute by raw pointer cast)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Oct 27, 2023
2 parents 54e57e6 + 2fdac63 commit 95f6a01
Show file tree
Hide file tree
Showing 32 changed files with 265 additions and 309 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ pub enum StashKey {
TraitMissingMethod,
OpaqueHiddenTypeMismatch,
MaybeForgetReturn,
/// Query cycle detected, stashing in favor of a better error.
Cycle,
}

fn default_track_diagnostic(d: &mut Diagnostic, f: &mut dyn FnMut(&mut Diagnostic)) {
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_macros/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ struct QueryModifiers {
/// A cycle error results in a delay_bug call
cycle_delay_bug: Option<Ident>,

/// A cycle error results in a stashed cycle error that can be unstashed and canceled later
cycle_stash: Option<Ident>,

/// Don't hash the result, instead just mark a query red if it runs
no_hash: Option<Ident>,

Expand Down Expand Up @@ -127,6 +130,7 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
let mut desc = None;
let mut fatal_cycle = None;
let mut cycle_delay_bug = None;
let mut cycle_stash = None;
let mut no_hash = None;
let mut anon = None;
let mut eval_always = None;
Expand Down Expand Up @@ -181,6 +185,8 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
try_insert!(fatal_cycle = modifier);
} else if modifier == "cycle_delay_bug" {
try_insert!(cycle_delay_bug = modifier);
} else if modifier == "cycle_stash" {
try_insert!(cycle_stash = modifier);
} else if modifier == "no_hash" {
try_insert!(no_hash = modifier);
} else if modifier == "anon" {
Expand Down Expand Up @@ -208,6 +214,7 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
desc,
fatal_cycle,
cycle_delay_bug,
cycle_stash,
no_hash,
anon,
eval_always,
Expand Down Expand Up @@ -329,6 +336,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
fatal_cycle,
arena_cache,
cycle_delay_bug,
cycle_stash,
no_hash,
anon,
eval_always,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ rustc_queries! {
"computing type of opaque `{path}`",
path = tcx.def_path_str(key),
}
cycle_stash
}

query type_alias_is_lazy(key: DefId) -> bool {
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ passes_invalid_attr_at_crate_level =
`{$name}` attribute cannot be used at crate level
.suggestion = perhaps you meant to use an outer attribute
passes_invalid_attr_at_crate_level_item =
the inner attribute doesn't annotate this {$kind}
passes_invalid_deprecation_version =
invalid deprecation version found
.label = invalid deprecation version
Expand Down
22 changes: 21 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2534,10 +2534,30 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
if attr.style == AttrStyle::Inner {
for attr_to_check in ATTRS_TO_CHECK {
if attr.has_name(*attr_to_check) {
let item = tcx
.hir()
.items()
.map(|id| tcx.hir().item(id))
.find(|item| !item.span.is_dummy()) // Skip prelude `use`s
.map(|item| errors::ItemFollowingInnerAttr {
span: item.ident.span,
kind: item.kind.descr(),
});
tcx.sess.emit_err(errors::InvalidAttrAtCrateLevel {
span: attr.span,
snippet: tcx.sess.source_map().span_to_snippet(attr.span).ok(),
sugg_span: tcx
.sess
.source_map()
.span_to_snippet(attr.span)
.ok()
.filter(|src| src.starts_with("#!["))
.map(|_| {
attr.span
.with_lo(attr.span.lo() + BytePos(1))
.with_hi(attr.span.lo() + BytePos(2))
}),
name: *attr_to_check,
item,
});
}
}
Expand Down
20 changes: 15 additions & 5 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,15 @@ pub struct UnknownLangItem {

pub struct InvalidAttrAtCrateLevel {
pub span: Span,
pub snippet: Option<String>,
pub sugg_span: Option<Span>,
pub name: Symbol,
pub item: Option<ItemFollowingInnerAttr>,
}

#[derive(Clone, Copy)]
pub struct ItemFollowingInnerAttr {
pub span: Span,
pub kind: &'static str,
}

impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
Expand All @@ -871,15 +878,18 @@ impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
diag.set_arg("name", self.name);
// Only emit an error with a suggestion if we can create a string out
// of the attribute span
if let Some(src) = self.snippet {
let replacement = src.replace("#!", "#");
if let Some(span) = self.sugg_span {
diag.span_suggestion_verbose(
self.span,
span,
fluent::passes_suggestion,
replacement,
String::new(),
rustc_errors::Applicability::MachineApplicable,
);
}
if let Some(item) = self.item {
diag.set_arg("kind", item.kind);
diag.span_label(item.span, fluent::passes_invalid_attr_at_crate_level_item);
}
diag
}
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ macro_rules! handle_cycle_error {
([(fatal_cycle) $($rest:tt)*]) => {{
rustc_query_system::HandleCycleError::Fatal
}};
([(cycle_stash) $($rest:tt)*]) => {{
rustc_query_system::HandleCycleError::Stash
}};
([(cycle_delay_bug) $($rest:tt)*]) => {{
rustc_query_system::HandleCycleError::DelayBug
}};
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_query_system/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum HandleCycleError {
Error,
Fatal,
DelayBug,
Stash,
}

#[derive(Subdiagnostic)]
Expand Down
13 changes: 12 additions & 1 deletion compiler/rustc_query_system/src/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_data_structures::sync::Lock;
#[cfg(parallel_compiler)]
use rustc_data_structures::{outline, sync};
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, FatalError};
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, FatalError, StashKey};
use rustc_span::{Span, DUMMY_SP};
use std::cell::Cell;
use std::collections::hash_map::Entry;
Expand Down Expand Up @@ -133,6 +133,17 @@ where
let guar = error.delay_as_bug();
query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle, guar)
}
Stash => {
let guar = if let Some(root) = cycle_error.cycle.first()
&& let Some(span) = root.query.span
{
error.stash(span, StashKey::Cycle);
qcx.dep_context().sess().delay_span_bug(span, "delayed cycle error")
} else {
error.emit()
};
query.value_from_cycle_error(*qcx.dep_context(), &cycle_error.cycle, guar)
}
}
}

Expand Down
19 changes: 14 additions & 5 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,11 +1283,15 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
let kind = match self.kind() {
ty::Value(val) => {
let const_val = tables.tcx.valtree_to_const_val((self.ty(), val));
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
self.ty(),
const_val,
tables,
))
if matches!(const_val, mir::ConstValue::ZeroSized) {
ConstantKind::ZeroSized
} else {
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
self.ty(),
const_val,
tables,
))
}
}
ty::ParamCt(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)),
ty::ErrorCt(_) => unreachable!(),
Expand Down Expand Up @@ -1401,6 +1405,11 @@ impl<'tcx> Stable<'tcx> for rustc_middle::mir::Const<'tcx> {
let id = tables.intern_const(*self);
Const::new(kind, ty, id)
}
mir::Const::Val(val, ty) if matches!(val, mir::ConstValue::ZeroSized) => {
let ty = ty.stable(tables);
let id = tables.intern_const(*self);
Const::new(ConstantKind::ZeroSized, ty, id)
}
mir::Const::Val(val, ty) => {
let kind = ConstantKind::Allocated(alloc::new_allocation(ty, val, tables));
let ty = ty.stable(tables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3104,6 +3104,13 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
);
}
};

if let Some(diag) =
self.tcx.sess.diagnostic().steal_diagnostic(self.tcx.def_span(def_id), StashKey::Cycle)
{
diag.cancel();
}

err
}

Expand Down
3 changes: 3 additions & 0 deletions compiler/stable_mir/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ pub enum ConstantKind {
Allocated(Allocation),
Unevaluated(UnevaluatedConst),
Param(ParamConst),
/// Store ZST constants.
/// We have to special handle these constants since its type might be generic.
ZeroSized,
}

#[derive(Clone, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/stable_mir/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Visitable for Const {
match &self.kind() {
super::ty::ConstantKind::Allocated(alloc) => alloc.visit(visitor)?,
super::ty::ConstantKind::Unevaluated(uv) => uv.visit(visitor)?,
super::ty::ConstantKind::Param(_) => {}
super::ty::ConstantKind::Param(_) | super::ty::ConstantKind::ZeroSized => {}
}
self.ty().visit(visitor)
}
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2142,7 +2142,7 @@ pub trait Iterator {
/// passed collection. The collection is then returned, so the call chain
/// can be continued.
///
/// This is useful when you already have a collection and wants to add
/// This is useful when you already have a collection and want to add
/// the iterator items to it.
///
/// This method is a convenience method to call [Extend::extend](trait.Extend.html),
Expand Down
16 changes: 8 additions & 8 deletions library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,13 @@ impl Builder {
scope_data.increment_num_running_threads();
}

let main = Box::new(main);
#[cfg(bootstrap)]
let main =
unsafe { mem::transmute::<Box<dyn FnOnce() + 'a>, Box<dyn FnOnce() + 'static>>(main) };
#[cfg(not(bootstrap))]
let main = unsafe { Box::from_raw(Box::into_raw(main) as *mut (dyn FnOnce() + 'static)) };

Ok(JoinInner {
// SAFETY:
//
Expand All @@ -559,14 +566,7 @@ impl Builder {
// Similarly, the `sys` implementation must guarantee that no references to the closure
// exist after the thread has terminated, which is signaled by `Thread::join`
// returning.
native: unsafe {
imp::Thread::new(
stack_size,
mem::transmute::<Box<dyn FnOnce() + 'a>, Box<dyn FnOnce() + 'static>>(
Box::new(main),
),
)?
},
native: unsafe { imp::Thread::new(stack_size, main)? },
thread: my_thread,
packet: my_packet,
})
Expand Down
23 changes: 23 additions & 0 deletions src/doc/rustc/src/profile-guided-optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,26 @@ in Clang's documentation is therefore an interesting read for anyone who wants
to use PGO with Rust.

[clang-pgo]: https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization

## Community Maintained Tools

As an alternative to directly using the compiler for Profile-Guided Optimization,
you may choose to go with `cargo-pgo`, which has an intuitive command-line API
and saves you the trouble of doing all the manual work. You can read more about
it in their repository accessible from this link: https://github.com/Kobzol/cargo-pgo

For the sake of completeness, here are the corresponding steps using `cargo-pgo`:

```bash
# Install if you haven't already
cargo install cargo-pgo

cargo pgo build
cargo pgo optimize
```

These steps will do the following just as before:

1. Build an instrumented binary from the source code.
2. Run the instrumented binary to gather PGO profiles.
3. Use the gathered PGO profiles from the last step to build an optimized binary.
8 changes: 6 additions & 2 deletions tests/ui/attributes/unix_sigpipe/unix_sigpipe-crate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ error: `unix_sigpipe` attribute cannot be used at crate level
|
LL | #![unix_sigpipe = "inherit"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL |
LL | fn main() {}
| ---- the inner attribute doesn't annotate this function
|
help: perhaps you meant to use an outer attribute
|
LL | #[unix_sigpipe = "inherit"]
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
LL - #![unix_sigpipe = "inherit"]
LL + #[unix_sigpipe = "inherit"]
|

error: aborting due to previous error

Loading

0 comments on commit 95f6a01

Please sign in to comment.