Skip to content

Commit ac0cb05

Browse files
committed
Auto merge of #144637 - Zalathar:rollup-t1yo1jy, r=Zalathar
Rollup of 6 pull requests Successful merges: - #144560 (coverage: Treat `#[automatically_derived]` as `#[coverage(off)]`) - #144566 (Simplify `align_of_val::<[T]>(…)` → `align_of::<T>()`) - #144587 (expand: Micro-optimize prelude injection) - #144589 (Account for `.yield` in illegal postfix operator message) - #144615 (Make resolve_fn_signature responsible for its own rib.) - #144634 (Fix typo in `DropGuard` doc) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 7278554 + 7088bf5 commit ac0cb05

File tree

76 files changed

+1100
-226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1100
-226
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,10 @@ pub enum DeprecatedSince {
110110
Err,
111111
}
112112

113-
#[derive(
114-
Copy,
115-
Debug,
116-
Eq,
117-
PartialEq,
118-
Encodable,
119-
Decodable,
120-
Clone,
121-
HashStable_Generic,
122-
PrintAttribute
123-
)]
124-
pub enum CoverageStatus {
113+
/// Successfully-parsed value of a `#[coverage(..)]` attribute.
114+
#[derive(Copy, Debug, Eq, PartialEq, Encodable, Decodable, Clone)]
115+
#[derive(HashStable_Generic, PrintAttribute)]
116+
pub enum CoverageAttrKind {
125117
On,
126118
Off,
127119
}
@@ -304,8 +296,8 @@ pub enum AttributeKind {
304296
/// Represents `#[const_trait]`.
305297
ConstTrait(Span),
306298

307-
/// Represents `#[coverage]`.
308-
Coverage(Span, CoverageStatus),
299+
/// Represents `#[coverage(..)]`.
300+
Coverage(Span, CoverageAttrKind),
309301

310302
///Represents `#[rustc_deny_explicit_impl]`.
311303
DenyExplicitImpl(Span),

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_attr_data_structures::{AttributeKind, CoverageStatus, OptimizeAttr, UsedBy};
1+
use rustc_attr_data_structures::{AttributeKind, CoverageAttrKind, OptimizeAttr, UsedBy};
22
use rustc_feature::{AttributeTemplate, template};
33
use rustc_session::parse::feature_err;
44
use rustc_span::{Span, Symbol, sym};
@@ -78,16 +78,16 @@ impl<S: Stage> SingleAttributeParser<S> for CoverageParser {
7878
return None;
7979
};
8080

81-
let status = match arg.path().word_sym() {
82-
Some(sym::off) => CoverageStatus::Off,
83-
Some(sym::on) => CoverageStatus::On,
81+
let kind = match arg.path().word_sym() {
82+
Some(sym::off) => CoverageAttrKind::Off,
83+
Some(sym::on) => CoverageAttrKind::On,
8484
None | Some(_) => {
8585
fail_incorrect_argument(arg.span());
8686
return None;
8787
}
8888
};
8989

90-
Some(AttributeKind::Coverage(cx.attr_span, status))
90+
Some(AttributeKind::Coverage(cx.attr_span, kind))
9191
}
9292
}
9393

compiler/rustc_builtin_macros/src/standard_library_imports.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ pub fn inject(
4747
ast::ItemKind::ExternCrate(None, Ident::new(name, ident_span)),
4848
);
4949

50-
krate.items.insert(0, item);
51-
5250
let root = (edition == Edition2015).then_some(kw::PathRoot);
5351

5452
let import_path = root
@@ -75,6 +73,6 @@ pub fn inject(
7573
}),
7674
);
7775

78-
krate.items.insert(0, use_item);
76+
krate.items.splice(0..0, [item, use_item]);
7977
krate.items.len() - orig_num_items
8078
}

compiler/rustc_mir_transform/src/coverage/query.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_attr_data_structures::{AttributeKind, CoverageStatus, find_attr};
1+
use rustc_attr_data_structures::{AttributeKind, CoverageAttrKind, find_attr};
22
use rustc_index::bit_set::DenseBitSet;
33
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
44
use rustc_middle::mir::coverage::{BasicCoverageBlock, CoverageIdsInfo, CoverageKind, MappingKind};
@@ -32,16 +32,6 @@ fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
3232
return false;
3333
}
3434

35-
// Don't instrument functions with `#[automatically_derived]` on their
36-
// enclosing impl block, on the assumption that most users won't care about
37-
// coverage for derived impls.
38-
if let Some(impl_of) = tcx.impl_of_assoc(def_id.to_def_id())
39-
&& tcx.is_automatically_derived(impl_of)
40-
{
41-
trace!("InstrumentCoverage skipped for {def_id:?} (automatically derived)");
42-
return false;
43-
}
44-
4535
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::NAKED) {
4636
trace!("InstrumentCoverage skipped for {def_id:?} (`#[naked]`)");
4737
return false;
@@ -57,20 +47,32 @@ fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
5747

5848
/// Query implementation for `coverage_attr_on`.
5949
fn coverage_attr_on(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
60-
// Check for annotations directly on this def.
61-
if let Some(coverage_status) =
62-
find_attr!(tcx.get_all_attrs(def_id), AttributeKind::Coverage(_, status) => status)
50+
// Check for a `#[coverage(..)]` attribute on this def.
51+
if let Some(kind) =
52+
find_attr!(tcx.get_all_attrs(def_id), AttributeKind::Coverage(_sp, kind) => kind)
6353
{
64-
*coverage_status == CoverageStatus::On
65-
} else {
66-
match tcx.opt_local_parent(def_id) {
67-
// Check the parent def (and so on recursively) until we find an
68-
// enclosing attribute or reach the crate root.
69-
Some(parent) => tcx.coverage_attr_on(parent),
70-
// We reached the crate root without seeing a coverage attribute, so
71-
// allow coverage instrumentation by default.
72-
None => true,
54+
match kind {
55+
CoverageAttrKind::On => return true,
56+
CoverageAttrKind::Off => return false,
7357
}
58+
};
59+
60+
// Treat `#[automatically_derived]` as an implied `#[coverage(off)]`, on
61+
// the assumption that most users won't want coverage for derived impls.
62+
//
63+
// This affects not just the associated items of an impl block, but also
64+
// any closures and other nested functions within those associated items.
65+
if tcx.is_automatically_derived(def_id.to_def_id()) {
66+
return false;
67+
}
68+
69+
// Check the parent def (and so on recursively) until we find an
70+
// enclosing attribute or reach the crate root.
71+
match tcx.opt_local_parent(def_id) {
72+
Some(parent) => tcx.coverage_attr_on(parent),
73+
// We reached the crate root without seeing a coverage attribute, so
74+
// allow coverage instrumentation by default.
75+
None => true,
7476
}
7577
}
7678

compiler/rustc_mir_transform/src/instsimplify.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
5555

5656
let terminator = block.terminator.as_mut().unwrap();
5757
ctx.simplify_primitive_clone(terminator, &mut block.statements);
58+
ctx.simplify_align_of_slice_val(terminator, &mut block.statements);
5859
ctx.simplify_intrinsic_assert(terminator);
5960
ctx.simplify_nounwind_call(terminator);
6061
simplify_duplicate_switch_targets(terminator);
@@ -252,6 +253,36 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
252253
terminator.kind = TerminatorKind::Goto { target: *destination_block };
253254
}
254255

256+
// Convert `align_of_val::<[T]>(ptr)` to `align_of::<T>()`, since the
257+
// alignment of a slice doesn't actually depend on metadata at all
258+
// and the element type is always `Sized`.
259+
//
260+
// This is here so it can run after inlining, where it's more useful.
261+
// (LowerIntrinsics is done in cleanup, before the optimization passes.)
262+
fn simplify_align_of_slice_val(
263+
&self,
264+
terminator: &mut Terminator<'tcx>,
265+
statements: &mut Vec<Statement<'tcx>>,
266+
) {
267+
if let TerminatorKind::Call {
268+
func, args, destination, target: Some(destination_block), ..
269+
} = &terminator.kind
270+
&& args.len() == 1
271+
&& let Some((fn_def_id, generics)) = func.const_fn_def()
272+
&& self.tcx.is_intrinsic(fn_def_id, sym::align_of_val)
273+
&& let ty::Slice(elem_ty) = *generics.type_at(0).kind()
274+
{
275+
statements.push(Statement::new(
276+
terminator.source_info,
277+
StatementKind::Assign(Box::new((
278+
*destination,
279+
Rvalue::NullaryOp(NullOp::AlignOf, elem_ty),
280+
))),
281+
));
282+
terminator.kind = TerminatorKind::Goto { target: *destination_block };
283+
}
284+
}
285+
255286
fn simplify_nounwind_call(&self, terminator: &mut Terminator<'tcx>) {
256287
let TerminatorKind::Call { ref func, ref mut unwind, .. } = terminator.kind else {
257288
return;

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,9 +785,13 @@ impl<'a> Parser<'a> {
785785
ExprKind::Call(_, _) => "a function call",
786786
ExprKind::Await(_, _) => "`.await`",
787787
ExprKind::Use(_, _) => "`.use`",
788+
ExprKind::Yield(YieldKind::Postfix(_)) => "`.yield`",
788789
ExprKind::Match(_, _, MatchKind::Postfix) => "a postfix match",
789790
ExprKind::Err(_) => return Ok(with_postfix),
790-
_ => unreachable!("parse_dot_or_call_expr_with_ shouldn't produce this"),
791+
_ => unreachable!(
792+
"did not expect {:?} as an illegal postfix operator following cast",
793+
with_postfix.kind
794+
),
791795
}
792796
);
793797
let mut err = self.dcx().struct_span_err(span, msg);

compiler/rustc_resolve/src/late.rs

Lines changed: 54 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -910,22 +910,15 @@ impl<'ast, 'ra, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'ra, 'tc
910910
span,
911911
|this| {
912912
this.visit_generic_params(&fn_ptr.generic_params, false);
913-
this.with_lifetime_rib(
914-
LifetimeRibKind::AnonymousCreateParameter {
915-
binder: ty.id,
916-
report_in_path: false,
917-
},
918-
|this| {
919-
this.resolve_fn_signature(
920-
ty.id,
921-
false,
922-
// We don't need to deal with patterns in parameters, because
923-
// they are not possible for foreign or bodiless functions.
924-
fn_ptr.decl.inputs.iter().map(|Param { ty, .. }| (None, &**ty)),
925-
&fn_ptr.decl.output,
926-
)
927-
},
928-
);
913+
this.resolve_fn_signature(
914+
ty.id,
915+
false,
916+
// We don't need to deal with patterns in parameters, because
917+
// they are not possible for foreign or bodiless functions.
918+
fn_ptr.decl.inputs.iter().map(|Param { ty, .. }| (None, &**ty)),
919+
&fn_ptr.decl.output,
920+
false,
921+
)
929922
},
930923
)
931924
}
@@ -1042,19 +1035,12 @@ impl<'ast, 'ra, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'ra, 'tc
10421035
self.visit_fn_header(&sig.header);
10431036
self.visit_ident(ident);
10441037
self.visit_generics(generics);
1045-
self.with_lifetime_rib(
1046-
LifetimeRibKind::AnonymousCreateParameter {
1047-
binder: fn_id,
1048-
report_in_path: false,
1049-
},
1050-
|this| {
1051-
this.resolve_fn_signature(
1052-
fn_id,
1053-
sig.decl.has_self(),
1054-
sig.decl.inputs.iter().map(|Param { ty, .. }| (None, &**ty)),
1055-
&sig.decl.output,
1056-
);
1057-
},
1038+
self.resolve_fn_signature(
1039+
fn_id,
1040+
sig.decl.has_self(),
1041+
sig.decl.inputs.iter().map(|Param { ty, .. }| (None, &**ty)),
1042+
&sig.decl.output,
1043+
false,
10581044
);
10591045
return;
10601046
}
@@ -1080,22 +1066,15 @@ impl<'ast, 'ra, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'ra, 'tc
10801066
.coroutine_kind
10811067
.map(|coroutine_kind| coroutine_kind.return_id());
10821068

1083-
this.with_lifetime_rib(
1084-
LifetimeRibKind::AnonymousCreateParameter {
1085-
binder: fn_id,
1086-
report_in_path: coro_node_id.is_some(),
1087-
},
1088-
|this| {
1089-
this.resolve_fn_signature(
1090-
fn_id,
1091-
declaration.has_self(),
1092-
declaration
1093-
.inputs
1094-
.iter()
1095-
.map(|Param { pat, ty, .. }| (Some(&**pat), &**ty)),
1096-
&declaration.output,
1097-
);
1098-
},
1069+
this.resolve_fn_signature(
1070+
fn_id,
1071+
declaration.has_self(),
1072+
declaration
1073+
.inputs
1074+
.iter()
1075+
.map(|Param { pat, ty, .. }| (Some(&**pat), &**ty)),
1076+
&declaration.output,
1077+
coro_node_id.is_some(),
10991078
);
11001079

11011080
if let Some(contract) = contract {
@@ -1307,19 +1286,12 @@ impl<'ast, 'ra, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'ra, 'tc
13071286
kind: LifetimeBinderKind::PolyTrait,
13081287
..
13091288
} => {
1310-
self.with_lifetime_rib(
1311-
LifetimeRibKind::AnonymousCreateParameter {
1312-
binder,
1313-
report_in_path: false,
1314-
},
1315-
|this| {
1316-
this.resolve_fn_signature(
1317-
binder,
1318-
false,
1319-
p_args.inputs.iter().map(|ty| (None, &**ty)),
1320-
&p_args.output,
1321-
)
1322-
},
1289+
self.resolve_fn_signature(
1290+
binder,
1291+
false,
1292+
p_args.inputs.iter().map(|ty| (None, &**ty)),
1293+
&p_args.output,
1294+
false,
13231295
);
13241296
break;
13251297
}
@@ -2236,25 +2208,32 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
22362208
has_self: bool,
22372209
inputs: impl Iterator<Item = (Option<&'ast Pat>, &'ast Ty)> + Clone,
22382210
output_ty: &'ast FnRetTy,
2211+
report_elided_lifetimes_in_path: bool,
22392212
) {
2240-
// Add each argument to the rib.
2241-
let elision_lifetime = self.resolve_fn_params(has_self, inputs);
2242-
debug!(?elision_lifetime);
2243-
2244-
let outer_failures = take(&mut self.diag_metadata.current_elision_failures);
2245-
let output_rib = if let Ok(res) = elision_lifetime.as_ref() {
2246-
self.r.lifetime_elision_allowed.insert(fn_id);
2247-
LifetimeRibKind::Elided(*res)
2248-
} else {
2249-
LifetimeRibKind::ElisionFailure
2213+
let rib = LifetimeRibKind::AnonymousCreateParameter {
2214+
binder: fn_id,
2215+
report_in_path: report_elided_lifetimes_in_path,
22502216
};
2251-
self.with_lifetime_rib(output_rib, |this| visit::walk_fn_ret_ty(this, output_ty));
2252-
let elision_failures =
2253-
replace(&mut self.diag_metadata.current_elision_failures, outer_failures);
2254-
if !elision_failures.is_empty() {
2255-
let Err(failure_info) = elision_lifetime else { bug!() };
2256-
self.report_missing_lifetime_specifiers(elision_failures, Some(failure_info));
2257-
}
2217+
self.with_lifetime_rib(rib, |this| {
2218+
// Add each argument to the rib.
2219+
let elision_lifetime = this.resolve_fn_params(has_self, inputs);
2220+
debug!(?elision_lifetime);
2221+
2222+
let outer_failures = take(&mut this.diag_metadata.current_elision_failures);
2223+
let output_rib = if let Ok(res) = elision_lifetime.as_ref() {
2224+
this.r.lifetime_elision_allowed.insert(fn_id);
2225+
LifetimeRibKind::Elided(*res)
2226+
} else {
2227+
LifetimeRibKind::ElisionFailure
2228+
};
2229+
this.with_lifetime_rib(output_rib, |this| visit::walk_fn_ret_ty(this, output_ty));
2230+
let elision_failures =
2231+
replace(&mut this.diag_metadata.current_elision_failures, outer_failures);
2232+
if !elision_failures.is_empty() {
2233+
let Err(failure_info) = elision_lifetime else { bug!() };
2234+
this.report_missing_lifetime_specifiers(elision_failures, Some(failure_info));
2235+
}
2236+
});
22582237
}
22592238

22602239
/// Resolve inside function parameters and parameter types.

library/core/src/mem/drop_guard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::ops::{Deref, DerefMut};
44

55
/// Wrap a value and run a closure when dropped.
66
///
7-
/// This is useful for quickly creating desructors inline.
7+
/// This is useful for quickly creating destructors inline.
88
///
99
/// # Examples
1010
///

0 commit comments

Comments
 (0)