Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit bd3cb52

Browse files
committed
Auto merge of rust-lang#92970 - matthiaskrgr:rollup-tcx7cfb, r=matthiaskrgr
Rollup of 10 pull requests Successful merges: - rust-lang#92487 (Fix unclosed boxes in pretty printing of TraitAlias) - rust-lang#92581 (ARMv6K Horizon - Enable default libraries) - rust-lang#92619 (Add diagnostic items for macros) - rust-lang#92635 (rustdoc: Yet more intra-doc links cleanup) - rust-lang#92646 (feat: rustc_pass_by_value lint attribute) - rust-lang#92706 (Clarify explicitly that BTree{Map,Set} are ordered.) - rust-lang#92710 (Include Projections when elaborating TypeOutlives) - rust-lang#92746 (Parse `Ty?` as `Option<Ty>` and provide structured suggestion) - rust-lang#92792 (rustdoc: fix intra-link for generic trait impls) - rust-lang#92814 (remove unused FIXME) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 48e89b0 + 2b6b49e commit bd3cb52

Some content is hidden

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

51 files changed

+906
-362
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,9 +1357,7 @@ impl<'a> State<'a> {
13571357
self.bclose(item.span, empty);
13581358
}
13591359
ast::ItemKind::TraitAlias(ref generics, ref bounds) => {
1360-
self.head("");
1361-
self.print_visibility(&item.vis);
1362-
self.word_nbsp("trait");
1360+
self.head(visibility_qualified(&item.vis, "trait"));
13631361
self.print_ident(item.ident);
13641362
self.print_generic_params(&generics.params);
13651363
let mut real_bounds = Vec::with_capacity(bounds.len());
@@ -1377,6 +1375,8 @@ impl<'a> State<'a> {
13771375
self.print_type_bounds("=", &real_bounds);
13781376
self.print_where_clause(&generics.where_clause);
13791377
self.word(";");
1378+
self.end(); // end inner head-block
1379+
self.end(); // end outer head-block
13801380
}
13811381
ast::ItemKind::MacCall(ref mac) => {
13821382
self.print_mac(mac);

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,11 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
623623
lang, Normal, template!(NameValueStr: "name"), DuplicatesOk, lang_items,
624624
"language items are subject to change",
625625
),
626+
rustc_attr!(
627+
rustc_pass_by_value, Normal,
628+
template!(Word), WarnFollowing,
629+
"#[rustc_pass_by_value] is used to mark types that must be passed by value instead of reference."
630+
),
626631
BuiltinAttribute {
627632
name: sym::rustc_diagnostic_item,
628633
type_: Normal,

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,9 +705,7 @@ impl<'a> State<'a> {
705705
self.bclose(item.span);
706706
}
707707
hir::ItemKind::TraitAlias(ref generics, ref bounds) => {
708-
self.head("");
709-
self.print_visibility(&item.vis);
710-
self.word_nbsp("trait");
708+
self.head(visibility_qualified(&item.vis, "trait"));
711709
self.print_ident(item.ident);
712710
self.print_generic_params(&generics.params);
713711
let mut real_bounds = Vec::with_capacity(bounds.len());
@@ -725,6 +723,8 @@ impl<'a> State<'a> {
725723
self.print_bounds("=", real_bounds);
726724
self.print_where_clause(&generics.where_clause);
727725
self.word(";");
726+
self.end(); // end inner head-block
727+
self.end(); // end outer head-block
728728
}
729729
}
730730
self.ann.post(self, AnnNode::Item(item))

compiler/rustc_index/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,3 @@
99
pub mod bit_set;
1010
pub mod interval;
1111
pub mod vec;
12-
13-
// FIXME(#56935): Work around ICEs during cross-compilation.
14-
#[allow(unused)]
15-
extern crate rustc_macros;

compiler/rustc_infer/src/infer/outlives/obligations.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
164164
"cannot process registered region obligations in a snapshot"
165165
);
166166

167-
debug!("process_registered_region_obligations()");
167+
debug!(?param_env, "process_registered_region_obligations()");
168168

169169
let my_region_obligations = self.take_registered_region_obligations();
170170

@@ -356,6 +356,8 @@ where
356356
let trait_bounds: Vec<_> =
357357
self.verify_bound.projection_declared_bounds_from_trait(projection_ty).collect();
358358

359+
debug!(?trait_bounds);
360+
359361
// Compute the bounds we can derive from the environment. This
360362
// is an "approximate" match -- in some cases, these bounds
361363
// may not apply.

compiler/rustc_infer/src/traits/util.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,19 @@ impl<'tcx> Elaborator<'tcx> {
241241

242242
Component::UnresolvedInferenceVariable(_) => None,
243243

244-
Component::Projection(_) | Component::EscapingProjection(_) => {
245-
// We can probably do more here. This
246-
// corresponds to a case like `<T as
247-
// Foo<'a>>::U: 'b`.
244+
Component::Projection(projection) => {
245+
// We might end up here if we have `Foo<<Bar as Baz>::Assoc>: 'a`.
246+
// With this, we can deduce that `<Bar as Baz>::Assoc: 'a`.
247+
let ty =
248+
tcx.mk_projection(projection.item_def_id, projection.substs);
249+
Some(ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(
250+
ty, r_min,
251+
)))
252+
}
253+
254+
Component::EscapingProjection(_) => {
255+
// We might be able to do more here, but we don't
256+
// want to deal with escaping vars right now.
248257
None
249258
}
250259
})

compiler/rustc_lint/src/internal.rs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}
55
use rustc_ast as ast;
66
use rustc_errors::Applicability;
77
use rustc_hir::def::Res;
8-
use rustc_hir::{
9-
GenericArg, HirId, Item, ItemKind, MutTy, Mutability, Node, Path, PathSegment, QPath, Ty,
10-
TyKind,
11-
};
8+
use rustc_hir::{GenericArg, HirId, Item, ItemKind, Node, Path, PathSegment, QPath, Ty, TyKind};
129
use rustc_middle::ty;
1310
use rustc_session::{declare_lint_pass, declare_tool_lint};
1411
use rustc_span::hygiene::{ExpnKind, MacroKind};
@@ -58,13 +55,6 @@ declare_tool_lint! {
5855
report_in_external_macro: true
5956
}
6057

61-
declare_tool_lint! {
62-
pub rustc::TY_PASS_BY_REFERENCE,
63-
Allow,
64-
"passing `Ty` or `TyCtxt` by reference",
65-
report_in_external_macro: true
66-
}
67-
6858
declare_tool_lint! {
6959
pub rustc::USAGE_OF_QUALIFIED_TY,
7060
Allow,
@@ -74,7 +64,6 @@ declare_tool_lint! {
7464

7565
declare_lint_pass!(TyTyKind => [
7666
USAGE_OF_TY_TYKIND,
77-
TY_PASS_BY_REFERENCE,
7867
USAGE_OF_QUALIFIED_TY,
7968
]);
8069

@@ -131,26 +120,6 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
131120
}
132121
}
133122
}
134-
TyKind::Rptr(_, MutTy { ty: inner_ty, mutbl: Mutability::Not }) => {
135-
if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner.to_def_id()) {
136-
if cx.tcx.impl_trait_ref(impl_did).is_some() {
137-
return;
138-
}
139-
}
140-
if let Some(t) = is_ty_or_ty_ctxt(cx, &inner_ty) {
141-
cx.struct_span_lint(TY_PASS_BY_REFERENCE, ty.span, |lint| {
142-
lint.build(&format!("passing `{}` by reference", t))
143-
.span_suggestion(
144-
ty.span,
145-
"try passing by value",
146-
t,
147-
// Changing type of function argument
148-
Applicability::MaybeIncorrect,
149-
)
150-
.emit();
151-
})
152-
}
153-
}
154123
_ => {}
155124
}
156125
}

compiler/rustc_lint/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ mod non_ascii_idents;
5656
mod non_fmt_panic;
5757
mod nonstandard_style;
5858
mod noop_method_call;
59+
mod pass_by_value;
5960
mod passes;
6061
mod redundant_semicolon;
6162
mod traits;
@@ -85,6 +86,7 @@ use non_ascii_idents::*;
8586
use non_fmt_panic::NonPanicFmt;
8687
use nonstandard_style::*;
8788
use noop_method_call::*;
89+
use pass_by_value::*;
8890
use redundant_semicolon::*;
8991
use traits::*;
9092
use types::*;
@@ -490,15 +492,17 @@ fn register_internals(store: &mut LintStore) {
490492
store.register_late_pass(|| Box::new(ExistingDocKeyword));
491493
store.register_lints(&TyTyKind::get_lints());
492494
store.register_late_pass(|| Box::new(TyTyKind));
495+
store.register_lints(&PassByValue::get_lints());
496+
store.register_late_pass(|| Box::new(PassByValue));
493497
store.register_group(
494498
false,
495499
"rustc::internal",
496500
None,
497501
vec![
498502
LintId::of(DEFAULT_HASH_TYPES),
499503
LintId::of(USAGE_OF_TY_TYKIND),
504+
LintId::of(PASS_BY_VALUE),
500505
LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
501-
LintId::of(TY_PASS_BY_REFERENCE),
502506
LintId::of(USAGE_OF_QUALIFIED_TY),
503507
LintId::of(EXISTING_DOC_KEYWORD),
504508
],
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use crate::{LateContext, LateLintPass, LintContext};
2+
use rustc_errors::Applicability;
3+
use rustc_hir as hir;
4+
use rustc_hir::def::Res;
5+
use rustc_hir::{GenericArg, PathSegment, QPath, TyKind};
6+
use rustc_middle::ty;
7+
use rustc_span::symbol::sym;
8+
9+
declare_tool_lint! {
10+
/// The `rustc_pass_by_value` lint marks a type with `#[rustc_pass_by_value]` requiring it to always be passed by value.
11+
/// This is usually used for types that are thin wrappers around references, so there is no benefit to an extra
12+
/// layer of indirection. (Example: `Ty` which is a reference to a `TyS`)
13+
pub rustc::PASS_BY_VALUE,
14+
Warn,
15+
"pass by reference of a type flagged as `#[rustc_pass_by_value]`",
16+
report_in_external_macro: true
17+
}
18+
19+
declare_lint_pass!(PassByValue => [PASS_BY_VALUE]);
20+
21+
impl<'tcx> LateLintPass<'tcx> for PassByValue {
22+
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx>) {
23+
match &ty.kind {
24+
TyKind::Rptr(_, hir::MutTy { ty: inner_ty, mutbl: hir::Mutability::Not }) => {
25+
if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner.to_def_id()) {
26+
if cx.tcx.impl_trait_ref(impl_did).is_some() {
27+
return;
28+
}
29+
}
30+
if let Some(t) = path_for_pass_by_value(cx, &inner_ty) {
31+
cx.struct_span_lint(PASS_BY_VALUE, ty.span, |lint| {
32+
lint.build(&format!("passing `{}` by reference", t))
33+
.span_suggestion(
34+
ty.span,
35+
"try passing by value",
36+
t,
37+
// Changing type of function argument
38+
Applicability::MaybeIncorrect,
39+
)
40+
.emit();
41+
})
42+
}
43+
}
44+
_ => {}
45+
}
46+
}
47+
}
48+
49+
fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<String> {
50+
if let TyKind::Path(QPath::Resolved(_, path)) = &ty.kind {
51+
match path.res {
52+
Res::Def(_, def_id) if cx.tcx.has_attr(def_id, sym::rustc_pass_by_value) => {
53+
let name = cx.tcx.item_name(def_id).to_ident_string();
54+
let path_segment = path.segments.last().unwrap();
55+
return Some(format!("{}{}", name, gen_args(cx, path_segment)));
56+
}
57+
Res::SelfTy(None, Some((did, _))) => {
58+
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
59+
if cx.tcx.has_attr(adt.did, sym::rustc_pass_by_value) {
60+
return Some(cx.tcx.def_path_str_with_substs(adt.did, substs));
61+
}
62+
}
63+
}
64+
_ => (),
65+
}
66+
}
67+
68+
None
69+
}
70+
71+
fn gen_args(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> String {
72+
if let Some(args) = &segment.args {
73+
let params = args
74+
.args
75+
.iter()
76+
.map(|arg| match arg {
77+
GenericArg::Lifetime(lt) => lt.name.ident().to_string(),
78+
GenericArg::Type(ty) => {
79+
cx.tcx.sess.source_map().span_to_snippet(ty.span).unwrap_or_default()
80+
}
81+
GenericArg::Const(c) => {
82+
cx.tcx.sess.source_map().span_to_snippet(c.span).unwrap_or_default()
83+
}
84+
GenericArg::Infer(_) => String::from("_"),
85+
})
86+
.collect::<Vec<_>>();
87+
88+
if !params.is_empty() {
89+
return format!("<{}>", params.join(", "));
90+
}
91+
}
92+
93+
String::new()
94+
}

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,7 @@ pub struct FreeRegionInfo {
961961
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/ty.html
962962
#[derive(Copy, Clone)]
963963
#[rustc_diagnostic_item = "TyCtxt"]
964+
#[cfg_attr(not(bootstrap), rustc_pass_by_value)]
964965
pub struct TyCtxt<'tcx> {
965966
gcx: &'tcx GlobalCtxt<'tcx>,
966967
}

0 commit comments

Comments
 (0)