Skip to content

Commit ad4ee06

Browse files
committed
Address PR comments
1 parent d64a998 commit ad4ee06

File tree

4 files changed

+28
-162
lines changed

4 files changed

+28
-162
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use hir::{BodyId, HirId};
4444
use rustc_abi::ExternAbi;
4545
use rustc_ast::*;
4646
use rustc_errors::ErrorGuaranteed;
47-
use rustc_hir::attrs::AttributeKind;
47+
use rustc_hir::attrs::{AttributeKind, InlineAttr};
4848
use rustc_hir::def_id::DefId;
4949
use rustc_middle::span_bug;
5050
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
@@ -105,27 +105,24 @@ impl<'hir> LoweringContext<'_, 'hir> {
105105

106106
fn add_inline_attribute_if_needed(&mut self, span: Span) {
107107
const PARENT_ID: hir::ItemLocalId = hir::ItemLocalId::ZERO;
108-
let create_inline_attr = || {
109-
hir::Attribute::Parsed(AttributeKind::Inline(
110-
rustc_hir::attrs::InlineAttr::Always,
111-
span,
112-
))
113-
};
108+
let create_inline_attr_slice =
109+
|| [hir::Attribute::Parsed(AttributeKind::Inline(InlineAttr::Hint, span))];
114110

115111
let new_attributes = match self.attrs.get(&PARENT_ID) {
116112
Some(attrs) => {
117113
// Check if reuse already specifies any inline attribute, if so, do nothing
118-
for attr in attrs.iter() {
119-
if let hir::Attribute::Parsed(AttributeKind::Inline(..)) = attr {
120-
return;
121-
}
114+
if attrs
115+
.iter()
116+
.any(|a| matches!(a, hir::Attribute::Parsed(AttributeKind::Inline(..))))
117+
{
118+
return;
122119
}
123120

124121
self.arena.alloc_from_iter(
125-
attrs.into_iter().map(|a| a.clone()).chain([create_inline_attr()]),
122+
attrs.into_iter().map(|a| a.clone()).chain(create_inline_attr_slice()),
126123
)
127124
}
128-
None => self.arena.alloc_from_iter([create_inline_attr()]),
125+
None => self.arena.alloc_from_iter(create_inline_attr_slice()),
129126
};
130127

131128
self.attrs.insert(PARENT_ID, new_attributes);

tests/pretty/delegation_inline_attribute.pp

Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
fn foo(x: usize) -> usize { x }
1414
}
1515

16-
// Check that #[inline(always)] is added to foo reuse
17-
#[attr = Inline(Always)]
16+
// Check that #[inline(hint)] is added to foo reuse
17+
#[attr = Inline(Hint)]
1818
fn bar(arg0: _) -> _ { to_reuse::foo(self + 1) }
1919

2020
trait Trait {
@@ -34,21 +34,21 @@
3434
}
3535

3636
impl Trait for S {
37-
// Check that #[inline(always)] is added to foo reuse
38-
#[attr = Inline(Always)]
37+
// Check that #[inline(hint)] is added to foo reuse
38+
#[attr = Inline(Hint)]
3939
fn foo(self: _)
4040
->
4141
_ {
4242
{
43-
// Check that #[inline(always)] is added to foo0 reuse inside another reuse
44-
#[attr = Inline(Always)]
43+
// Check that #[inline(hint)] is added to foo0 reuse inside another reuse
44+
#[attr = Inline(Hint)]
4545
fn foo0(arg0: _) -> _ { to_reuse::foo(self + 1) }
4646

47-
// Check that #[inline(always)] is added when other attributes present in inner reuse
47+
// Check that #[inline(hint)] is added when other attributes present in inner reuse
4848
#[attr = Deprecation {deprecation: Deprecation {since: Unspecified}}]
4949
#[attr = MustUse]
5050
#[attr = Cold]
51-
#[attr = Inline(Always)]
51+
#[attr = Inline(Hint)]
5252
fn foo1(arg0: _) -> _ { to_reuse::foo(self / 2) }
5353

5454
// Check that #[inline(never)] is preserved in inner reuse
@@ -68,11 +68,11 @@
6868
}.foo()
6969
}
7070

71-
// Check that #[inline(always)] is added when there are other attributes present in trait reuse
71+
// Check that #[inline(hint)] is added when there are other attributes present in trait reuse
7272
#[attr = Deprecation {deprecation: Deprecation {since: Unspecified}}]
7373
#[attr = MustUse]
7474
#[attr = Cold]
75-
#[attr = Inline(Always)]
75+
#[attr = Inline(Hint)]
7676
fn foo1(self: _) -> _ { self.0.foo1() }
7777

7878
// Check that #[inline(never)] is preserved in trait reuse
@@ -91,64 +91,4 @@
9191
fn foo4(self: _) -> _ { self.0.foo4() }
9292
}
9393

94-
fn main() {
95-
// Check that #[inline(always)] is added when there are no other attributes
96-
#[attr = Inline(Always)]
97-
fn foo(arg0: _) -> _ { to_reuse::foo(self / 2) }
98-
99-
// Check that #[inline(always)] is added when there are other attributes present
100-
#[attr = Deprecation {deprecation: Deprecation {since: Unspecified}}]
101-
#[attr = MustUse]
102-
#[attr = Cold]
103-
#[attr = Inline(Always)]
104-
fn foo1(arg0: _) -> _ { to_reuse::foo(self / 2) }
105-
106-
// Check that #[inline(never)] is preserved
107-
#[attr = Inline(Never)]
108-
fn foo2(arg0: _) -> _ { to_reuse::foo(self / 2) }
109-
110-
// Check that #[inline(always)] is preserved
111-
#[attr = Inline(Always)]
112-
fn foo3(arg0: _) -> _ { to_reuse::foo(self / 2) }
113-
114-
// Check that #[inline(never)] is preserved when there are other attributes
115-
#[attr = Deprecation {deprecation: Deprecation {since: Unspecified}}]
116-
#[attr = Inline(Never)]
117-
#[attr = MustUse]
118-
#[attr = Cold]
119-
fn foo4(arg0: _) -> _ { to_reuse::foo(self / 2) }
120-
121-
let x =
122-
||
123-
{
124-
// Check that #[inline(always)] is added when there are no other attributes
125-
#[attr = Inline(Always)]
126-
fn foo(arg0: _) -> _ { to_reuse::foo(self / 2) }
127-
128-
// Check that #[inline(always)] is added when there are other attributes present
129-
#[attr = Deprecation {deprecation: Deprecation {since: Unspecified}}]
130-
#[attr = MustUse]
131-
#[attr = Cold]
132-
#[attr = Inline(Always)]
133-
fn foo1(arg0: _) -> _ { to_reuse::foo(self / 2) }
134-
135-
// Check that #[inline(never)] is preserved
136-
#[attr = Inline(Never)]
137-
fn foo2(arg0: _) -> _ { to_reuse::foo(self / 2) }
138-
139-
// Check that #[inline(always)] is preserved
140-
#[attr = Inline(Always)]
141-
fn foo3(arg0: _) -> _ { to_reuse::foo(self / 2) }
142-
143-
let y =
144-
||
145-
{
146-
// Check that #[inline(never)] is preserved when there are other attributes
147-
#[attr = Deprecation {deprecation: Deprecation {since: Unspecified}}]
148-
#[attr = Inline(Never)]
149-
#[attr = MustUse]
150-
#[attr = Cold]
151-
fn foo4(arg0: _) -> _ { to_reuse::foo(self / 2) }
152-
};
153-
};
154-
}
94+
fn main() { }

tests/pretty/delegation_inline_attribute.rs

Lines changed: 5 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod to_reuse {
1111
}
1212
}
1313

14-
// Check that #[inline(always)] is added to foo reuse
14+
// Check that #[inline(hint)] is added to foo reuse
1515
reuse to_reuse::foo as bar {
1616
self + 1
1717
}
@@ -33,14 +33,14 @@ mod to_import {
3333
}
3434

3535
impl Trait for S {
36-
// Check that #[inline(always)] is added to foo reuse
36+
// Check that #[inline(hint)] is added to foo reuse
3737
reuse Trait::foo {
38-
// Check that #[inline(always)] is added to foo0 reuse inside another reuse
38+
// Check that #[inline(hint)] is added to foo0 reuse inside another reuse
3939
reuse to_reuse::foo as foo0 {
4040
self + 1
4141
}
4242

43-
// Check that #[inline(always)] is added when other attributes present in inner reuse
43+
// Check that #[inline(hint)] is added when other attributes present in inner reuse
4444
#[cold]
4545
#[must_use]
4646
#[deprecated]
@@ -70,7 +70,7 @@ impl Trait for S {
7070
}
7171
}
7272

73-
// Check that #[inline(always)] is added when there are other attributes present in trait reuse
73+
// Check that #[inline(hint)] is added when there are other attributes present in trait reuse
7474
#[cold]
7575
#[must_use]
7676
#[deprecated]
@@ -101,75 +101,4 @@ impl Trait for S {
101101
}
102102

103103
fn main() {
104-
// Check that #[inline(always)] is added when there are no other attributes
105-
reuse to_reuse::foo {
106-
self / 2
107-
}
108-
109-
// Check that #[inline(always)] is added when there are other attributes present
110-
#[cold]
111-
#[must_use]
112-
#[deprecated]
113-
reuse to_reuse::foo as foo1 {
114-
self / 2
115-
}
116-
117-
// Check that #[inline(never)] is preserved
118-
#[inline(never)]
119-
reuse to_reuse::foo as foo2 {
120-
self / 2
121-
}
122-
123-
// Check that #[inline(always)] is preserved
124-
#[inline(always)]
125-
reuse to_reuse::foo as foo3 {
126-
self / 2
127-
}
128-
129-
// Check that #[inline(never)] is preserved when there are other attributes
130-
#[cold]
131-
#[must_use]
132-
#[inline(never)]
133-
#[deprecated]
134-
reuse to_reuse::foo as foo4 {
135-
self / 2
136-
}
137-
138-
let x = || {
139-
// Check that #[inline(always)] is added when there are no other attributes
140-
reuse to_reuse::foo {
141-
self / 2
142-
}
143-
144-
// Check that #[inline(always)] is added when there are other attributes present
145-
#[cold]
146-
#[must_use]
147-
#[deprecated]
148-
reuse to_reuse::foo as foo1 {
149-
self / 2
150-
}
151-
152-
// Check that #[inline(never)] is preserved
153-
#[inline(never)]
154-
reuse to_reuse::foo as foo2 {
155-
self / 2
156-
}
157-
158-
// Check that #[inline(always)] is preserved
159-
#[inline(always)]
160-
reuse to_reuse::foo as foo3 {
161-
self / 2
162-
}
163-
164-
let y = || {
165-
// Check that #[inline(never)] is preserved when there are other attributes
166-
#[cold]
167-
#[must_use]
168-
#[inline(never)]
169-
#[deprecated]
170-
reuse to_reuse::foo as foo4 {
171-
self / 2
172-
}
173-
};
174-
};
175104
}

tests/pretty/hir-delegation.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
fn b<C>(e: C) { }
1313

1414
trait G {
15-
#[attr = Inline(Always)]
15+
#[attr = Inline(Hint)]
1616
fn b(arg0: _) -> _ { b({ }) }
1717
}
1818

1919
mod m {
2020
fn add(a: u32, b: u32) -> u32 { a + b }
2121
}
2222

23-
#[attr = Inline(Always)]
23+
#[attr = Inline(Hint)]
2424
fn add(arg0: _, arg1: _) -> _ { m::add(arg0, arg1) }
2525

2626
fn main() { { let _ = add(1, 2); }; }

0 commit comments

Comments
 (0)