Skip to content

Commit 11e5b6d

Browse files
Rollup merge of rust-lang#153029 - nnethercote:disallowed-pass-by-ref, r=Urgau
Rename `rustc::pass_by_value` lint as `rustc::disallowed_pass_by_ref`. The name `pass_by_value` is completely wrong. The lint actually checks for the use of pass by reference for types marked with `rustc_pass_by_value`. The hardest part of this was choosing the new name. The `disallowed_` part of the name closely matches the following clippy lints: - `disallowed_macros` - `disallowed_methods` - `disallowed_names` - `disallowed_script_idents` - `disallowed_types` The `pass_by_value` part of the name aligns with the following clippy lints: - `needless_pass_by_value` - `needless_pass_by_ref_mut` - `trivially_copy_pass_by_ref` - `large_types_passed_by_value` (less so) r? @Urgau
2 parents 1c0ddee + 8eed8bd commit 11e5b6d

File tree

10 files changed

+35
-34
lines changed

10 files changed

+35
-34
lines changed

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,9 +757,11 @@ macro_rules! common_visitor_and_walkers {
757757
) -> V::Result;
758758
}
759759

760-
// this is only used by the MutVisitor. We include this symmetry here to make writing other functions easier
760+
// This is only used by the MutVisitor. We include this symmetry here to make writing other
761+
// functions easier.
761762
$(${ignore($lt)}
762-
#[expect(unused, rustc::pass_by_value)]
763+
#[cfg_attr(not(bootstrap), expect(unused, rustc::disallowed_pass_by_ref))]
764+
#[cfg_attr(bootstrap, expect(unused, rustc::pass_by_value))]
763765
#[inline]
764766
)?
765767
fn visit_span<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, span: &$($lt)? $($mut)? Span) -> V::Result {

compiler/rustc_lint/src/pass_by_value.rs renamed to compiler/rustc_lint/src/disallowed_pass_by_ref.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,34 @@ use rustc_hir::{self as hir, AmbigArg, GenericArg, PathSegment, QPath, TyKind, f
33
use rustc_middle::ty;
44
use rustc_session::{declare_lint_pass, declare_tool_lint};
55

6-
use crate::lints::PassByValueDiag;
6+
use crate::lints::DisallowedPassByRefDiag;
77
use crate::{LateContext, LateLintPass, LintContext};
88

99
declare_tool_lint! {
10-
/// The `rustc_pass_by_value` lint marks a type with `#[rustc_pass_by_value]` requiring it to
11-
/// always be passed by value. This is usually used for types that are thin wrappers around
12-
/// references, so there is no benefit to an extra layer of indirection. (Example: `Ty` which
13-
/// is a reference to an `Interned<TyKind>`)
14-
pub rustc::PASS_BY_VALUE,
10+
/// The `disallowed_pass_by_ref` lint detects if types marked with `#[rustc_pass_by_value]` are
11+
/// passed by reference. Types with this marker are usually thin wrappers around references, so
12+
/// there is no benefit to an extra layer of indirection. (Example: `Ty` which is a reference
13+
/// to an `Interned<TyKind>`)
14+
pub rustc::DISALLOWED_PASS_BY_REF,
1515
Warn,
1616
"pass by reference of a type flagged as `#[rustc_pass_by_value]`",
1717
report_in_external_macro: true
1818
}
1919

20-
declare_lint_pass!(PassByValue => [PASS_BY_VALUE]);
20+
declare_lint_pass!(DisallowedPassByRef => [DISALLOWED_PASS_BY_REF]);
2121

22-
impl<'tcx> LateLintPass<'tcx> for PassByValue {
22+
impl<'tcx> LateLintPass<'tcx> for DisallowedPassByRef {
2323
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx, AmbigArg>) {
2424
match &ty.kind {
2525
TyKind::Ref(_, hir::MutTy { ty: inner_ty, mutbl: hir::Mutability::Not }) => {
2626
if cx.tcx.trait_impl_of_assoc(ty.hir_id.owner.to_def_id()).is_some() {
2727
return;
2828
}
29-
if let Some(t) = path_for_pass_by_value(cx, inner_ty) {
29+
if let Some(t) = path_for_rustc_pass_by_value(cx, inner_ty) {
3030
cx.emit_span_lint(
31-
PASS_BY_VALUE,
31+
DISALLOWED_PASS_BY_REF,
3232
ty.span,
33-
PassByValueDiag { ty: t, suggestion: ty.span },
33+
DisallowedPassByRefDiag { ty: t, suggestion: ty.span },
3434
);
3535
}
3636
}
@@ -39,7 +39,7 @@ impl<'tcx> LateLintPass<'tcx> for PassByValue {
3939
}
4040
}
4141

42-
fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<String> {
42+
fn path_for_rustc_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<String> {
4343
if let TyKind::Path(QPath::Resolved(_, path)) = &ty.kind {
4444
match path.res {
4545
Res::Def(_, def_id) if find_attr!(cx.tcx, def_id, RustcPassByValue(_)) => {

compiler/rustc_lint/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ mod context;
3737
mod dangling;
3838
mod default_could_be_derived;
3939
mod deref_into_dyn_supertrait;
40+
mod disallowed_pass_by_ref;
4041
mod drop_forget_useless;
4142
mod early;
4243
mod enum_intrinsics_non_enums;
@@ -65,7 +66,6 @@ mod non_local_def;
6566
mod nonstandard_style;
6667
mod noop_method_call;
6768
mod opaque_hidden_inferred_bound;
68-
mod pass_by_value;
6969
mod passes;
7070
mod precedence;
7171
mod ptr_nulls;
@@ -88,6 +88,7 @@ use builtin::*;
8888
use dangling::*;
8989
use default_could_be_derived::DefaultCouldBeDerived;
9090
use deref_into_dyn_supertrait::*;
91+
use disallowed_pass_by_ref::*;
9192
use drop_forget_useless::*;
9293
use enum_intrinsics_non_enums::EnumIntrinsicsNonEnums;
9394
use for_loops_over_fallibles::*;
@@ -109,7 +110,6 @@ use non_local_def::*;
109110
use nonstandard_style::*;
110111
use noop_method_call::*;
111112
use opaque_hidden_inferred_bound::*;
112-
use pass_by_value::*;
113113
use precedence::*;
114114
use ptr_nulls::*;
115115
use redundant_semicolon::*;
@@ -659,8 +659,8 @@ fn register_internals(store: &mut LintStore) {
659659
store.register_late_mod_pass(|_| Box::new(TypeIr));
660660
store.register_lints(&BadOptAccess::lint_vec());
661661
store.register_late_mod_pass(|_| Box::new(BadOptAccess));
662-
store.register_lints(&PassByValue::lint_vec());
663-
store.register_late_mod_pass(|_| Box::new(PassByValue));
662+
store.register_lints(&DisallowedPassByRef::lint_vec());
663+
store.register_late_mod_pass(|_| Box::new(DisallowedPassByRef));
664664
store.register_lints(&SpanUseEqCtxt::lint_vec());
665665
store.register_late_mod_pass(|_| Box::new(SpanUseEqCtxt));
666666
store.register_lints(&SymbolInternStringLiteral::lint_vec());
@@ -678,7 +678,7 @@ fn register_internals(store: &mut LintStore) {
678678
LintId::of(POTENTIAL_QUERY_INSTABILITY),
679679
LintId::of(UNTRACKED_QUERY_INFORMATION),
680680
LintId::of(USAGE_OF_TY_TYKIND),
681-
LintId::of(PASS_BY_VALUE),
681+
LintId::of(DISALLOWED_PASS_BY_REF),
682682
LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
683683
LintId::of(USAGE_OF_QUALIFIED_TY),
684684
LintId::of(NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT),

compiler/rustc_lint/src/lints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,10 +1807,10 @@ pub(crate) struct AmbiguousNegativeLiteralsCurrentBehaviorSuggestion {
18071807
pub end_span: Span,
18081808
}
18091809

1810-
// pass_by_value.rs
1810+
// disallowed_pass_by_ref.rs
18111811
#[derive(LintDiagnostic)]
18121812
#[diag("passing `{$ty}` by reference")]
1813-
pub(crate) struct PassByValueDiag {
1813+
pub(crate) struct DisallowedPassByRefDiag {
18141814
pub ty: String,
18151815
#[suggestion("try passing by value", code = "{ty}", applicability = "maybe-incorrect")]
18161816
pub suggestion: Span,

compiler/rustc_macros/src/query.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,10 @@ fn make_helpers_for_query(query: &Query, streams: &mut HelperTokenStreams) {
293293

294294
// Generate a function to check whether we should cache the query to disk, for some key.
295295
if let Some(CacheOnDiskIf { block, .. }) = modifiers.cache_on_disk_if.as_ref() {
296-
// `pass_by_value`: some keys are marked with `rustc_pass_by_value`, but we take keys by
297-
// reference here.
298-
// FIXME: `pass_by_value` is badly named; `allow(rustc::pass_by_value)` actually means
299-
// "allow pass by reference of `rustc_pass_by_value` types".
296+
// `disallowed_pass_by_ref` is needed because some keys are `rustc_pass_by_value`.
300297
streams.cache_on_disk_if_fns_stream.extend(quote! {
301-
#[allow(unused_variables, rustc::pass_by_value)]
298+
#[cfg_attr(not(bootstrap), allow(unused_variables, rustc::disallowed_pass_by_ref))]
299+
#[cfg_attr(bootstrap, allow(unused_variables, rustc::pass_by_value))]
302300
#[inline]
303301
pub fn #erased_name<'tcx>(tcx: TyCtxt<'tcx>, #key_pat: &#key_ty) -> bool
304302
#block

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ impl<'a, 'tcx> ValueSet<'a, 'tcx> {
302302

303303
/// Insert a `(Value, Ty)` pair to be deduplicated.
304304
/// Returns `true` as second tuple field if this value did not exist previously.
305-
#[allow(rustc::pass_by_value)] // closures take `&VnIndex`
305+
#[cfg_attr(not(bootstrap), allow(rustc::disallowed_pass_by_ref))] // closures take `&VnIndex`
306+
#[cfg_attr(bootstrap, allow(rustc::pass_by_value))]
306307
fn insert(&mut self, ty: Ty<'tcx>, value: Value<'a, 'tcx>) -> (VnIndex, bool) {
307308
debug_assert!(match value {
308309
Value::Opaque(_) | Value::Address { .. } => false,

tests/ui-fulldeps/internal-lints/rustc_pass_by_value.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//@ compile-flags: -Z unstable-options
2-
2+
//@ ignore-stage1 (this can be removed when nightly goes to 1.96)
33
#![feature(rustc_attrs)]
44
#![feature(rustc_private)]
5-
#![deny(rustc::pass_by_value)]
5+
#![deny(rustc::disallowed_pass_by_ref)]
66
#![allow(unused)]
77

88
extern crate rustc_middle;

tests/ui-fulldeps/internal-lints/rustc_pass_by_value.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ LL | ty_ref: &Ty<'_>,
77
note: the lint level is defined here
88
--> $DIR/rustc_pass_by_value.rs:5:9
99
|
10-
LL | #![deny(rustc::pass_by_value)]
11-
| ^^^^^^^^^^^^^^^^^^^^
10+
LL | #![deny(rustc::disallowed_pass_by_ref)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: passing `TyCtxt<'_>` by reference
1414
--> $DIR/rustc_pass_by_value.rs:16:18

tests/ui/internal-lints/rustc_pass_by_value_self.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Considering that all other `internal-lints` are tested here
66
// this seems like the cleaner solution though.
77
#![feature(rustc_attrs)]
8-
#![deny(rustc::pass_by_value)]
8+
#![deny(rustc::disallowed_pass_by_ref)]
99
#![allow(unused)]
1010

1111
#[rustc_pass_by_value]

tests/ui/internal-lints/rustc_pass_by_value_self.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ LL | fn by_ref(&self) {}
77
note: the lint level is defined here
88
--> $DIR/rustc_pass_by_value_self.rs:8:9
99
|
10-
LL | #![deny(rustc::pass_by_value)]
11-
| ^^^^^^^^^^^^^^^^^^^^
10+
LL | #![deny(rustc::disallowed_pass_by_ref)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: passing `Ty<'tcx>` by reference
1414
--> $DIR/rustc_pass_by_value_self.rs:30:21

0 commit comments

Comments
 (0)