Skip to content

Commit 0b45675

Browse files
committed
Auto merge of #139169 - matthiaskrgr:rollup-nfy4aew, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #138176 (Prefer built-in sized impls (and only sized impls) for rigid types always) - #138749 (Fix closure recovery for missing block when return type is specified) - #138842 (Emit `unused_attributes` for `#[inline]` on exported functions) - #139153 (Encode synthetic by-move coroutine body with a different `DefPathData`) - #139157 (Remove mention of `exhaustive_patterns` from `never` docs) - #139167 (Remove Amanieu from the libs review rotation) r? `@ghost` `@rustbot` modify labels: rollup
2 parents ab5b1be + 70a1bbc commit 0b45675

File tree

43 files changed

+478
-73
lines changed

Some content is hidden

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

43 files changed

+478
-73
lines changed

compiler/rustc_hir/src/def.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ impl DefKind {
294294
DefKind::GlobalAsm => DefPathData::GlobalAsm,
295295
DefKind::Impl { .. } => DefPathData::Impl,
296296
DefKind::Closure => DefPathData::Closure,
297-
DefKind::SyntheticCoroutineBody => DefPathData::Closure,
297+
DefKind::SyntheticCoroutineBody => DefPathData::SyntheticCoroutineBody,
298298
}
299299
}
300300

compiler/rustc_hir/src/definitions.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ pub enum DefPathData {
291291
/// An existential `impl Trait` type node.
292292
/// Argument position `impl Trait` have a `TypeNs` with their pretty-printed name.
293293
OpaqueTy,
294+
/// A synthetic body for a coroutine's by-move body.
295+
SyntheticCoroutineBody,
294296
}
295297

296298
impl Definitions {
@@ -415,8 +417,16 @@ impl DefPathData {
415417

416418
ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
417419

418-
Impl | ForeignMod | CrateRoot | Use | GlobalAsm | Closure | Ctor | AnonConst
419-
| OpaqueTy => None,
420+
Impl
421+
| ForeignMod
422+
| CrateRoot
423+
| Use
424+
| GlobalAsm
425+
| Closure
426+
| Ctor
427+
| AnonConst
428+
| OpaqueTy
429+
| SyntheticCoroutineBody => None,
420430
}
421431
}
422432

@@ -441,6 +451,7 @@ impl DefPathData {
441451
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
442452
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
443453
OpaqueTy => DefPathDataName::Anon { namespace: sym::opaque },
454+
SyntheticCoroutineBody => DefPathDataName::Anon { namespace: sym::synthetic },
444455
}
445456
}
446457
}

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

+2
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ impl CodegenFnAttrs {
172172
/// * `#[no_mangle]` is present
173173
/// * `#[export_name(...)]` is present
174174
/// * `#[linkage]` is present
175+
///
176+
/// Keep this in sync with the logic for the unused_attributes for `#[inline]` lint.
175177
pub fn contains_extern_indicator(&self) -> bool {
176178
self.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
177179
|| self.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)

compiler/rustc_middle/src/mir/mono.rs

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ impl<'tcx> MonoItem<'tcx> {
150150

151151
// If the function is #[naked] or contains any other attribute that requires exactly-once
152152
// instantiation:
153+
// We emit an unused_attributes lint for this case, which should be kept in sync if possible.
153154
let codegen_fn_attrs = tcx.codegen_fn_attrs(instance.def_id());
154155
if codegen_fn_attrs.contains_extern_indicator()
155156
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED)

compiler/rustc_middle/src/traits/select.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,16 @@ pub type EvaluationCache<'tcx, ENV> = Cache<(ENV, ty::PolyTraitPredicate<'tcx>),
9595
/// parameter environment.
9696
#[derive(PartialEq, Eq, Debug, Clone, TypeVisitable)]
9797
pub enum SelectionCandidate<'tcx> {
98+
/// A built-in implementation for the `Sized` trait. This is preferred
99+
/// over all other candidates.
100+
SizedCandidate {
101+
has_nested: bool,
102+
},
103+
98104
/// A builtin implementation for some specific traits, used in cases
99105
/// where we cannot rely an ordinary library implementations.
100106
///
101-
/// The most notable examples are `sized`, `Copy` and `Clone`. This is also
107+
/// The most notable examples are `Copy` and `Clone`. This is also
102108
/// used for the `DiscriminantKind` and `Pointee` trait, both of which have
103109
/// an associated type.
104110
BuiltinCandidate {

compiler/rustc_middle/src/ty/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1930,10 +1930,10 @@ impl<'tcx> TyCtxt<'tcx> {
19301930
// As a consequence, this LocalDefId is always re-created before it is needed by the incr.
19311931
// comp. engine itself.
19321932
//
1933-
// This call also writes to the value of `source_span` and `expn_that_defined` queries.
1933+
// This call also writes to the value of the `source_span` query.
19341934
// This is fine because:
1935-
// - those queries are `eval_always` so we won't miss their result changing;
1936-
// - this write will have happened before these queries are called.
1935+
// - that query is `eval_always` so we won't miss its result changing;
1936+
// - this write will have happened before that query is called.
19371937
let def_id = self.untracked.definitions.write().create_def(parent, data);
19381938

19391939
// This function modifies `self.definitions` using a side-effect.

compiler/rustc_middle/src/ty/print/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ pub trait Printer<'tcx>: Sized {
139139

140140
match key.disambiguated_data.data {
141141
DefPathData::Closure => {
142-
// FIXME(async_closures): This is somewhat ugly.
143-
// We need to additionally print the `kind` field of a closure if
142+
// We need to additionally print the `kind` field of a coroutine if
144143
// it is desugared from a coroutine-closure.
145144
if let Some(hir::CoroutineKind::Desugared(
146145
_,
@@ -156,6 +155,10 @@ pub trait Printer<'tcx>: Sized {
156155
// Closures' own generics are only captures, don't print them.
157156
}
158157
}
158+
DefPathData::SyntheticCoroutineBody => {
159+
// Synthetic coroutine bodies have no distinct generics, since like
160+
// closures they're all just internal state of the coroutine.
161+
}
159162
// This covers both `DefKind::AnonConst` and `DefKind::InlineConst`.
160163
// Anon consts doesn't have their own generics, and inline consts' own
161164
// generics are their inferred types, so don't print them.

compiler/rustc_parse/src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -810,16 +810,16 @@ pub(crate) enum WrapInParentheses {
810810

811811
#[derive(Diagnostic)]
812812
#[diag(parse_array_brackets_instead_of_braces)]
813-
pub(crate) struct ArrayBracketsInsteadOfSpaces {
813+
pub(crate) struct ArrayBracketsInsteadOfBraces {
814814
#[primary_span]
815815
pub span: Span,
816816
#[subdiagnostic]
817-
pub sub: ArrayBracketsInsteadOfSpacesSugg,
817+
pub sub: ArrayBracketsInsteadOfBracesSugg,
818818
}
819819

820820
#[derive(Subdiagnostic)]
821821
#[multipart_suggestion(parse_suggestion, applicability = "maybe-incorrect")]
822-
pub(crate) struct ArrayBracketsInsteadOfSpacesSugg {
822+
pub(crate) struct ArrayBracketsInsteadOfBracesSugg {
823823
#[suggestion_part(code = "[")]
824824
pub left: Span,
825825
#[suggestion_part(code = "]")]

compiler/rustc_parse/src/parser/expr.rs

+52-9
Original file line numberDiff line numberDiff line change
@@ -2200,7 +2200,9 @@ impl<'a> Parser<'a> {
22002200
}
22012201

22022202
fn is_array_like_block(&mut self) -> bool {
2203-
self.look_ahead(1, |t| matches!(t.kind, TokenKind::Ident(..) | TokenKind::Literal(_)))
2203+
matches!(self.token.kind, TokenKind::OpenDelim(Delimiter::Brace))
2204+
&& self
2205+
.look_ahead(1, |t| matches!(t.kind, TokenKind::Ident(..) | TokenKind::Literal(_)))
22042206
&& self.look_ahead(2, |t| t == &token::Comma)
22052207
&& self.look_ahead(3, |t| t.can_begin_expr())
22062208
}
@@ -2212,9 +2214,9 @@ impl<'a> Parser<'a> {
22122214
let mut snapshot = self.create_snapshot_for_diagnostic();
22132215
match snapshot.parse_expr_array_or_repeat(exp!(CloseBrace)) {
22142216
Ok(arr) => {
2215-
let guar = self.dcx().emit_err(errors::ArrayBracketsInsteadOfSpaces {
2217+
let guar = self.dcx().emit_err(errors::ArrayBracketsInsteadOfBraces {
22162218
span: arr.span,
2217-
sub: errors::ArrayBracketsInsteadOfSpacesSugg {
2219+
sub: errors::ArrayBracketsInsteadOfBracesSugg {
22182220
left: lo,
22192221
right: snapshot.prev_token.span,
22202222
},
@@ -2337,7 +2339,8 @@ impl<'a> Parser<'a> {
23372339
let capture_clause = self.parse_capture_clause()?;
23382340
let (fn_decl, fn_arg_span) = self.parse_fn_block_decl()?;
23392341
let decl_hi = self.prev_token.span;
2340-
let mut body = match fn_decl.output {
2342+
let mut body = match &fn_decl.output {
2343+
// No return type.
23412344
FnRetTy::Default(_) => {
23422345
let restrictions =
23432346
self.restrictions - Restrictions::STMT_EXPR - Restrictions::ALLOW_LET;
@@ -2349,11 +2352,8 @@ impl<'a> Parser<'a> {
23492352
Err(err) => self.recover_closure_body(err, before, prev, token, lo, decl_hi)?,
23502353
}
23512354
}
2352-
_ => {
2353-
// If an explicit return type is given, require a block to appear (RFC 968).
2354-
let body_lo = self.token.span;
2355-
self.parse_expr_block(None, body_lo, BlockCheckMode::Default)?
2356-
}
2355+
// Explicit return type (`->`) needs block `-> T { }`.
2356+
FnRetTy::Ty(ty) => self.parse_closure_block_body(ty.span)?,
23572357
};
23582358

23592359
match coroutine_kind {
@@ -2405,6 +2405,49 @@ impl<'a> Parser<'a> {
24052405
Ok(closure)
24062406
}
24072407

2408+
/// If an explicit return type is given, require a block to appear (RFC 968).
2409+
fn parse_closure_block_body(&mut self, ret_span: Span) -> PResult<'a, P<Expr>> {
2410+
if self.may_recover()
2411+
&& self.token.can_begin_expr()
2412+
&& !matches!(self.token.kind, TokenKind::OpenDelim(Delimiter::Brace))
2413+
&& !self.token.is_whole_block()
2414+
{
2415+
let snapshot = self.create_snapshot_for_diagnostic();
2416+
let restrictions =
2417+
self.restrictions - Restrictions::STMT_EXPR - Restrictions::ALLOW_LET;
2418+
let tok = self.token.clone();
2419+
match self.parse_expr_res(restrictions, AttrWrapper::empty()) {
2420+
Ok((expr, _)) => {
2421+
let descr = super::token_descr(&tok);
2422+
let mut diag = self
2423+
.dcx()
2424+
.struct_span_err(tok.span, format!("expected `{{`, found {descr}"));
2425+
diag.span_label(
2426+
ret_span,
2427+
"explicit return type requires closure body to be enclosed in braces",
2428+
);
2429+
diag.multipart_suggestion_verbose(
2430+
"wrap the expression in curly braces",
2431+
vec![
2432+
(expr.span.shrink_to_lo(), "{ ".to_string()),
2433+
(expr.span.shrink_to_hi(), " }".to_string()),
2434+
],
2435+
Applicability::MachineApplicable,
2436+
);
2437+
diag.emit();
2438+
return Ok(expr);
2439+
}
2440+
Err(diag) => {
2441+
diag.cancel();
2442+
self.restore_snapshot(snapshot);
2443+
}
2444+
}
2445+
}
2446+
2447+
let body_lo = self.token.span;
2448+
self.parse_expr_block(None, body_lo, BlockCheckMode::Default)
2449+
}
2450+
24082451
/// Parses an optional `move` or `use` prefix to a closure-like construct.
24092452
fn parse_capture_clause(&mut self) -> PResult<'a, CaptureBy> {
24102453
if self.eat_keyword(exp!(Move)) {

compiler/rustc_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ passes_inline_ignored_constants =
383383
.warn = {-passes_previously_accepted}
384384
.note = {-passes_see_issue(issue: "65833")}
385385
386+
passes_inline_ignored_for_exported =
387+
`#[inline]` is ignored on externally exported functions
388+
.help = externally exported functions are functions with `#[no_mangle]`, `#[export_name]`, or `#[linkage]`
389+
386390
passes_inline_ignored_function_prototype =
387391
`#[inline]` is ignored on function prototypes
388392

compiler/rustc_passes/src/check_attr.rs

+17
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,23 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
451451
});
452452
}
453453
}
454+
455+
// `#[inline]` is ignored if the symbol must be codegened upstream because it's exported.
456+
if let Some(did) = hir_id.as_owner()
457+
&& self.tcx.def_kind(did).has_codegen_attrs()
458+
&& !matches!(attr.meta_item_list().as_deref(), Some([item]) if item.has_name(sym::never))
459+
{
460+
let attrs = self.tcx.codegen_fn_attrs(did);
461+
// Not checking naked as `#[inline]` is forbidden for naked functions anyways.
462+
if attrs.contains_extern_indicator() {
463+
self.tcx.emit_node_span_lint(
464+
UNUSED_ATTRIBUTES,
465+
hir_id,
466+
attr.span(),
467+
errors::InlineIgnoredForExported {},
468+
);
469+
}
470+
}
454471
}
455472

456473
/// Checks that `#[coverage(..)]` is applied to a function/closure/method,

compiler/rustc_passes/src/errors.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,11 @@ pub(crate) struct OnlyHasEffectOn {
14411441
pub target_name: String,
14421442
}
14431443

1444+
#[derive(LintDiagnostic)]
1445+
#[diag(passes_inline_ignored_for_exported)]
1446+
#[help]
1447+
pub(crate) struct InlineIgnoredForExported {}
1448+
14441449
#[derive(Diagnostic)]
14451450
#[diag(passes_object_lifetime_err)]
14461451
pub(crate) struct ObjectLifetimeErr {

compiler/rustc_query_system/src/dep_graph/graph.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub struct MarkFrame<'a> {
6666
parent: Option<&'a MarkFrame<'a>>,
6767
}
6868

69+
#[derive(Debug)]
6970
pub(super) enum DepNodeColor {
7071
Red,
7172
Green(DepNodeIndex),
@@ -909,7 +910,7 @@ impl<D: Deps> DepGraphData<D> {
909910
self.try_mark_previous_green(qcx, parent_dep_node_index, dep_dep_node, frame);
910911

911912
if node_index.is_some() {
912-
debug!("managed to MARK dependency {dep_dep_node:?} as green",);
913+
debug!("managed to MARK dependency {dep_dep_node:?} as green");
913914
return Some(());
914915
}
915916
}
@@ -930,7 +931,7 @@ impl<D: Deps> DepGraphData<D> {
930931
return Some(());
931932
}
932933
Some(DepNodeColor::Red) => {
933-
debug!("dependency {dep_dep_node:?} was red after forcing",);
934+
debug!("dependency {dep_dep_node:?} was red after forcing");
934935
return None;
935936
}
936937
None => {}
@@ -950,7 +951,7 @@ impl<D: Deps> DepGraphData<D> {
950951
// invalid state will not be persisted to the
951952
// incremental compilation cache because of
952953
// compilation errors being present.
953-
debug!("dependency {dep_dep_node:?} resulted in compilation error",);
954+
debug!("dependency {dep_dep_node:?} resulted in compilation error");
954955
return None;
955956
}
956957

compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/encode.rs

+1
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
716716
hir::definitions::DefPathData::Ctor => "c",
717717
hir::definitions::DefPathData::AnonConst => "k",
718718
hir::definitions::DefPathData::OpaqueTy => "i",
719+
hir::definitions::DefPathData::SyntheticCoroutineBody => "s",
719720
hir::definitions::DefPathData::CrateRoot
720721
| hir::definitions::DefPathData::Use
721722
| hir::definitions::DefPathData::GlobalAsm

compiler/rustc_symbol_mangling/src/legacy.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ pub(super) fn mangle<'tcx>(
2828
loop {
2929
let key = tcx.def_key(ty_def_id);
3030
match key.disambiguated_data.data {
31-
DefPathData::TypeNs(_) | DefPathData::ValueNs(_) | DefPathData::Closure => {
31+
DefPathData::TypeNs(_)
32+
| DefPathData::ValueNs(_)
33+
| DefPathData::Closure
34+
| DefPathData::SyntheticCoroutineBody => {
3235
instance_ty = tcx.type_of(ty_def_id).instantiate_identity();
3336
debug!(?instance_ty);
3437
break;

compiler/rustc_symbol_mangling/src/v0.rs

+1
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
850850
DefPathData::Ctor => 'c',
851851
DefPathData::AnonConst => 'k',
852852
DefPathData::OpaqueTy => 'i',
853+
DefPathData::SyntheticCoroutineBody => 's',
853854

854855
// These should never show up as `path_append` arguments.
855856
DefPathData::CrateRoot

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
8686
// `Pointee` is automatically implemented for every type.
8787
candidates.vec.push(BuiltinCandidate { has_nested: false });
8888
} else if tcx.is_lang_item(def_id, LangItem::Sized) {
89-
// Sized is never implementable by end-users, it is
90-
// always automatically computed.
91-
let sized_conditions = self.sized_conditions(obligation);
92-
self.assemble_builtin_bound_candidates(sized_conditions, &mut candidates);
89+
self.assemble_builtin_sized_candidate(obligation, &mut candidates);
9390
} else if tcx.is_lang_item(def_id, LangItem::Unsize) {
9491
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
9592
} else if tcx.is_lang_item(def_id, LangItem::Destruct) {
@@ -1061,6 +1058,27 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10611058
/// Assembles the trait which are built-in to the language itself:
10621059
/// `Copy`, `Clone` and `Sized`.
10631060
#[instrument(level = "debug", skip(self, candidates))]
1061+
fn assemble_builtin_sized_candidate(
1062+
&mut self,
1063+
obligation: &PolyTraitObligation<'tcx>,
1064+
candidates: &mut SelectionCandidateSet<'tcx>,
1065+
) {
1066+
match self.sized_conditions(obligation) {
1067+
BuiltinImplConditions::Where(nested) => {
1068+
candidates
1069+
.vec
1070+
.push(SizedCandidate { has_nested: !nested.skip_binder().is_empty() });
1071+
}
1072+
BuiltinImplConditions::None => {}
1073+
BuiltinImplConditions::Ambiguous => {
1074+
candidates.ambiguous = true;
1075+
}
1076+
}
1077+
}
1078+
1079+
/// Assembles the trait which are built-in to the language itself:
1080+
/// e.g. `Copy` and `Clone`.
1081+
#[instrument(level = "debug", skip(self, candidates))]
10641082
fn assemble_builtin_bound_candidates(
10651083
&mut self,
10661084
conditions: BuiltinImplConditions<'tcx>,

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
4040
candidate: SelectionCandidate<'tcx>,
4141
) -> Result<Selection<'tcx>, SelectionError<'tcx>> {
4242
let mut impl_src = match candidate {
43+
SizedCandidate { has_nested } => {
44+
let data = self.confirm_builtin_candidate(obligation, has_nested);
45+
ImplSource::Builtin(BuiltinImplSource::Misc, data)
46+
}
47+
4348
BuiltinCandidate { has_nested } => {
4449
let data = self.confirm_builtin_candidate(obligation, has_nested);
4550
ImplSource::Builtin(BuiltinImplSource::Misc, data)

0 commit comments

Comments
 (0)