Skip to content

Commit ef4dece

Browse files
committed
Port #[rustc_pass_by_value] to the new attribute system
1 parent 35453a8 commit ef4dece

File tree

7 files changed

+24
-7
lines changed

7 files changed

+24
-7
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ pub enum AttributeKind {
284284
/// Represents `#[optimize(size|speed)]`
285285
Optimize(OptimizeAttr, Span),
286286

287+
/// Represents `#[rustc_pass_by_value]` (used by the `rustc_pass_by_value` lint).
288+
PassByValue(Span),
289+
287290
/// Represents `#[rustc_pub_transparent]` (used by the `repr_transparent_external_private_fields` lint).
288291
PubTransparent(Span),
289292

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl AttributeKind {
3636
Naked(..) => No,
3737
NoMangle(..) => No,
3838
Optimize(..) => No,
39+
PassByValue(..) => Yes,
3940
PubTransparent(..) => Yes,
4041
Repr(..) => No,
4142
RustcLayoutScalarValidRangeEnd(..) => Yes,

compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ impl<S: Stage> NoArgsAttributeParser<S> for PubTransparentParser {
1717
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1818
const CREATE: fn(Span) -> AttributeKind = AttributeKind::PubTransparent;
1919
}
20+
21+
pub(crate) struct PassByValueParser;
22+
impl<S: Stage> NoArgsAttributeParser<S> for PassByValueParser {
23+
const PATH: &[Symbol] = &[sym::rustc_pass_by_value];
24+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
25+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::PassByValue;
26+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::attributes::confusables::ConfusablesParser;
2323
use crate::attributes::deprecation::DeprecationParser;
2424
use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
2525
use crate::attributes::link_attrs::{LinkNameParser, LinkSectionParser};
26-
use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser};
26+
use crate::attributes::lint_helpers::{AsPtrParser, PassByValueParser, PubTransparentParser};
2727
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
2828
use crate::attributes::must_use::MustUseParser;
2929
use crate::attributes::repr::{AlignParser, ReprParser};
@@ -142,6 +142,7 @@ attribute_parsers!(
142142
Single<WithoutArgs<LoopMatchParser>>,
143143
Single<WithoutArgs<MayDangleParser>>,
144144
Single<WithoutArgs<NoMangleParser>>,
145+
Single<WithoutArgs<PassByValueParser>>,
145146
Single<WithoutArgs<PubTransparentParser>>,
146147
Single<WithoutArgs<TrackCallerParser>>,
147148
// tidy-alphabetical-end

compiler/rustc_lint/src/pass_by_value.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use rustc_attr_data_structures::{AttributeKind, find_attr};
12
use rustc_hir::def::Res;
23
use rustc_hir::{self as hir, AmbigArg, GenericArg, PathSegment, QPath, TyKind};
34
use rustc_middle::ty;
45
use rustc_session::{declare_lint_pass, declare_tool_lint};
5-
use rustc_span::sym;
66

77
use crate::lints::PassByValueDiag;
88
use crate::{LateContext, LateLintPass, LintContext};
@@ -45,14 +45,16 @@ impl<'tcx> LateLintPass<'tcx> for PassByValue {
4545
fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<String> {
4646
if let TyKind::Path(QPath::Resolved(_, path)) = &ty.kind {
4747
match path.res {
48-
Res::Def(_, def_id) if cx.tcx.has_attr(def_id, sym::rustc_pass_by_value) => {
48+
Res::Def(_, def_id)
49+
if find_attr!(cx.tcx.get_all_attrs(def_id), AttributeKind::PassByValue(_)) =>
50+
{
4951
let name = cx.tcx.item_ident(def_id);
5052
let path_segment = path.segments.last().unwrap();
5153
return Some(format!("{}{}", name, gen_args(cx, path_segment)));
5254
}
5355
Res::SelfTyAlias { alias_to: did, is_trait_impl: false, .. } => {
5456
if let ty::Adt(adt, args) = cx.tcx.type_of(did).instantiate_identity().kind() {
55-
if cx.tcx.has_attr(adt.did(), sym::rustc_pass_by_value) {
57+
if find_attr!(cx.tcx.get_all_attrs(adt.did()), AttributeKind::PassByValue(_)) {
5658
return Some(cx.tcx.def_path_str_with_args(adt.did(), args));
5759
}
5860
}

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ fn emit_malformed_attribute(
293293
| sym::rustc_force_inline
294294
| sym::rustc_confusables
295295
| sym::rustc_skip_during_method_dispatch
296+
| sym::rustc_pass_by_value
296297
| sym::repr
297298
| sym::align
298299
| sym::deprecated

compiler/rustc_passes/src/check_attr.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
213213
Attribute::Parsed(AttributeKind::Used { span: attr_span, .. }) => {
214214
self.check_used(*attr_span, target, span);
215215
}
216+
&Attribute::Parsed(AttributeKind::PassByValue(attr_span)) => {
217+
self.check_pass_by_value(attr_span, span, target)
218+
}
216219
Attribute::Unparsed(attr_item) => {
217220
style = Some(attr_item.style);
218221
match attr.path().as_slice() {
@@ -275,7 +278,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
275278
| [sym::const_trait, ..] => self.check_must_be_applied_to_trait(attr.span(), span, target),
276279
[sym::collapse_debuginfo, ..] => self.check_collapse_debuginfo(attr, span, target),
277280
[sym::must_not_suspend, ..] => self.check_must_not_suspend(attr, span, target),
278-
[sym::rustc_pass_by_value, ..] => self.check_pass_by_value(attr, span, target),
279281
[sym::rustc_allow_incoherent_impl, ..] => {
280282
self.check_allow_incoherent_impl(attr, span, target)
281283
}
@@ -1438,11 +1440,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
14381440
}
14391441

14401442
/// Warns against some misuses of `#[pass_by_value]`
1441-
fn check_pass_by_value(&self, attr: &Attribute, span: Span, target: Target) {
1443+
fn check_pass_by_value(&self, attr_span: Span, span: Span, target: Target) {
14421444
match target {
14431445
Target::Struct | Target::Enum | Target::TyAlias => {}
14441446
_ => {
1445-
self.dcx().emit_err(errors::PassByValue { attr_span: attr.span(), span });
1447+
self.dcx().emit_err(errors::PassByValue { attr_span, span });
14461448
}
14471449
}
14481450
}

0 commit comments

Comments
 (0)