Skip to content

Commit e8566fb

Browse files
committed
Move handling of #[track_caller] to check_attr
1 parent b925eb5 commit e8566fb

14 files changed

+95
-124
lines changed

src/librustc/error_codes.rs

+52-1
Original file line numberDiff line numberDiff line change
@@ -2219,7 +2219,7 @@ rejected in your own crates.
22192219
"##,
22202220

22212221
E0736: r##"
2222-
#[track_caller] and #[naked] cannot be applied to the same function.
2222+
`#[track_caller]` and `#[naked]` cannot both be applied to the same function.
22232223
22242224
Erroneous code example:
22252225
@@ -2237,6 +2237,57 @@ See [RFC 2091] for details on this and other limitations.
22372237
[RFC 2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md
22382238
"##,
22392239

2240+
E0738: r##"
2241+
`#[track_caller]` cannot be used in traits yet. This is due to limitations in
2242+
the compiler which are likely to be temporary. See [RFC 2091] for details on
2243+
this and other restrictions.
2244+
2245+
Erroneous example with a trait method implementation:
2246+
2247+
```compile_fail,E0738
2248+
#![feature(track_caller)]
2249+
2250+
trait Foo {
2251+
fn bar(&self);
2252+
}
2253+
2254+
impl Foo for u64 {
2255+
#[track_caller]
2256+
fn bar(&self) {}
2257+
}
2258+
```
2259+
2260+
Erroneous example with a blanket trait method implementation:
2261+
2262+
```compile_fail,E0738
2263+
#![feature(track_caller)]
2264+
2265+
trait Foo {
2266+
#[track_caller]
2267+
fn bar(&self) {}
2268+
fn baz(&self);
2269+
}
2270+
```
2271+
2272+
Erroneous example with a trait method declaration:
2273+
2274+
```compile_fail,E0738
2275+
#![feature(track_caller)]
2276+
2277+
trait Foo {
2278+
fn bar(&self) {}
2279+
2280+
#[track_caller]
2281+
fn baz(&self);
2282+
}
2283+
```
2284+
2285+
Note that while the compiler may be able to support the attribute in traits in
2286+
the future, [RFC 2091] prohibits their implementation without a follow-up RFC.
2287+
2288+
[RFC 2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md
2289+
"##,
2290+
22402291
;
22412292
// E0006, // merged with E0005
22422293
// E0101, // replaced with E0282

src/librustc/hir/check_attr.rs

+31-21
Original file line numberDiff line numberDiff line change
@@ -206,27 +206,37 @@ impl CheckAttrVisitor<'tcx> {
206206
span: &Span,
207207
target: Target,
208208
) -> bool {
209-
if target != Target::Fn {
210-
struct_span_err!(
211-
self.tcx.sess,
212-
*attr_span,
213-
E0739,
214-
"attribute should be applied to function"
215-
)
216-
.span_label(*span, "not a function")
217-
.emit();
218-
false
219-
} else if attr::contains_name(attrs, sym::naked) {
220-
struct_span_err!(
221-
self.tcx.sess,
222-
*attr_span,
223-
E0736,
224-
"cannot use `#[track_caller]` with `#[naked]`",
225-
)
226-
.emit();
227-
false
228-
} else {
229-
true
209+
match target {
210+
Target::Fn if attr::contains_name(attrs, sym::naked) => {
211+
struct_span_err!(
212+
self.tcx.sess,
213+
*attr_span,
214+
E0736,
215+
"cannot use `#[track_caller]` with `#[naked]`",
216+
).emit();
217+
false
218+
}
219+
Target::Fn => true,
220+
Target::Method { .. } => {
221+
struct_span_err!(
222+
self.tcx.sess,
223+
*attr_span,
224+
E0738,
225+
"`#[track_caller]` may not be used on trait methods",
226+
).emit();
227+
false
228+
}
229+
_ => {
230+
struct_span_err!(
231+
self.tcx.sess,
232+
*attr_span,
233+
E0739,
234+
"attribute should be applied to function"
235+
)
236+
.span_label(*span, "not a function")
237+
.emit();
238+
false
239+
}
230240
}
231241
}
232242

src/librustc/hir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2756,10 +2756,10 @@ bitflags! {
27562756
/// `#[used]`: indicates that LLVM can't eliminate this function (but the
27572757
/// linker can!).
27582758
const USED = 1 << 9;
2759-
/// #[ffi_returns_twice], indicates that an extern function can return
2759+
/// `#[ffi_returns_twice]`, indicates that an extern function can return
27602760
/// multiple times
27612761
const FFI_RETURNS_TWICE = 1 << 10;
2762-
/// #[track_caller]: allow access to the caller location
2762+
/// `#[track_caller]`: allow access to the caller location
27632763
const TRACK_CALLER = 1 << 11;
27642764
}
27652765
}

src/librustc_typeck/check/wfcheck.rs

-35
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,6 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: DefId) {
172172
_ => None
173173
};
174174
check_associated_item(tcx, trait_item.hir_id, trait_item.span, method_sig);
175-
176-
// Prohibits applying `#[track_caller]` to trait decls
177-
for attr in &trait_item.attrs {
178-
if attr.check_name(sym::track_caller) {
179-
struct_span_err!(
180-
tcx.sess,
181-
attr.span,
182-
E0738,
183-
"`#[track_caller]` is not supported in trait declarations."
184-
).emit();
185-
}
186-
}
187175
}
188176

189177
pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: DefId) {
@@ -195,29 +183,6 @@ pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: DefId) {
195183
_ => None
196184
};
197185

198-
// Prohibits applying `#[track_caller]` to trait impls
199-
if method_sig.is_some() {
200-
let track_caller_attr = impl_item.attrs.iter()
201-
.find(|a| a.check_name(sym::track_caller));
202-
if let Some(tc_attr) = track_caller_attr {
203-
let parent_hir_id = tcx.hir().get_parent_item(hir_id);
204-
let containing_item = tcx.hir().expect_item(parent_hir_id);
205-
let containing_impl_is_for_trait = match &containing_item.kind {
206-
hir::ItemKind::Impl(_, _, _, _, tr, _, _) => tr.is_some(),
207-
_ => bug!("parent of an ImplItem must be an Impl"),
208-
};
209-
210-
if containing_impl_is_for_trait {
211-
struct_span_err!(
212-
tcx.sess,
213-
tc_attr.span,
214-
E0738,
215-
"`#[track_caller]` is not supported in traits yet."
216-
).emit();
217-
}
218-
}
219-
}
220-
221186
check_associated_item(tcx, impl_item.hir_id, impl_item.span, method_sig);
222187
}
223188

src/librustc_typeck/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2640,7 +2640,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
26402640
tcx.sess,
26412641
attr.span,
26422642
E0737,
2643-
"rust ABI is required to use `#[track_caller]`"
2643+
"Rust ABI is required to use `#[track_caller]`"
26442644
).emit();
26452645
}
26462646
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;

src/librustc_typeck/error_codes.rs

+1-52
Original file line numberDiff line numberDiff line change
@@ -4938,7 +4938,7 @@ and the pin is required to keep it in the same place in memory.
49384938
"##,
49394939

49404940
E0737: r##"
4941-
#[track_caller] requires functions to have the "Rust" ABI for implicitly
4941+
`#[track_caller]` requires functions to have the `"Rust"` ABI for implicitly
49424942
receiving caller location. See [RFC 2091] for details on this and other
49434943
restrictions.
49444944
@@ -4954,57 +4954,6 @@ extern "C" fn foo() {}
49544954
[RFC 2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md
49554955
"##,
49564956

4957-
E0738: r##"
4958-
#[track_caller] cannot be used in traits yet. This is due to limitations in the
4959-
compiler which are likely to be temporary. See [RFC 2091] for details on this
4960-
and other restrictions.
4961-
4962-
Erroneous example with a trait method implementation:
4963-
4964-
```compile_fail,E0738
4965-
#![feature(track_caller)]
4966-
4967-
trait Foo {
4968-
fn bar(&self);
4969-
}
4970-
4971-
impl Foo for u64 {
4972-
#[track_caller]
4973-
fn bar(&self) {}
4974-
}
4975-
```
4976-
4977-
Erroneous example with a blanket trait method implementation:
4978-
4979-
```compile_fail,E0738
4980-
#![feature(track_caller)]
4981-
4982-
trait Foo {
4983-
#[track_caller]
4984-
fn bar(&self) {}
4985-
fn baz(&self);
4986-
}
4987-
```
4988-
4989-
Erroneous example with a trait method declaration:
4990-
4991-
```compile_fail,E0738
4992-
#![feature(track_caller)]
4993-
4994-
trait Foo {
4995-
fn bar(&self) {}
4996-
4997-
#[track_caller]
4998-
fn baz(&self);
4999-
}
5000-
```
5001-
5002-
Note that while the compiler may be able to support the attribute in traits in
5003-
the future, [RFC 2091] prohibits their implementation without a follow-up RFC.
5004-
5005-
[RFC 2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md
5006-
"##,
5007-
50084957
E0741: r##"
50094958
Only `structural_match` types (that is, types that derive `PartialEq` and `Eq`)
50104959
may be used as the types of const generic parameters.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![feature(track_caller)] //~ WARN the feature `track_caller` is incomplete
22

3-
#[track_caller]
3+
#[track_caller] //~ ERROR Rust ABI is required to use `#[track_caller]`
44
extern "C" fn f() {}
5-
//~^^ ERROR rust ABI is required to use `#[track_caller]`
65

76
fn main() {}

src/test/ui/rfc-2091-track-caller/error-with-invalid-abi.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | #![feature(track_caller)]
66
|
77
= note: `#[warn(incomplete_features)]` on by default
88

9-
error[E0737]: rust ABI is required to use `#[track_caller]`
9+
error[E0737]: Rust ABI is required to use `#[track_caller]`
1010
--> $DIR/error-with-invalid-abi.rs:3:1
1111
|
1212
LL | #[track_caller]

src/test/ui/rfc-2091-track-caller/error-with-trait-decl.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#![feature(track_caller)] //~ WARN the feature `track_caller` is incomplete
22

33
trait Trait {
4-
#[track_caller]
4+
#[track_caller] //~ ERROR: `#[track_caller]` may not be used on trait methods
55
fn unwrap(&self);
6-
//~^^ ERROR: `#[track_caller]` is not supported in trait declarations.
76
}
87

98
impl Trait for u64 {

src/test/ui/rfc-2091-track-caller/error-with-trait-decl.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | #![feature(track_caller)]
66
|
77
= note: `#[warn(incomplete_features)]` on by default
88

9-
error[E0738]: `#[track_caller]` is not supported in trait declarations.
9+
error[E0738]: `#[track_caller]` may not be used on trait methods
1010
--> $DIR/error-with-trait-decl.rs:4:5
1111
|
1212
LL | #[track_caller]
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#![feature(track_caller)] //~ WARN the feature `track_caller` is incomplete
22

33
trait Trait {
4-
#[track_caller]
4+
#[track_caller] //~ ERROR: `#[track_caller]` may not be used on trait methods
55
fn unwrap(&self) {}
6-
//~^^ ERROR: `#[track_caller]` is not supported in trait declarations.
76
}
87

98
fn main() {}

src/test/ui/rfc-2091-track-caller/error-with-trait-default-impl.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | #![feature(track_caller)]
66
|
77
= note: `#[warn(incomplete_features)]` on by default
88

9-
error[E0738]: `#[track_caller]` is not supported in trait declarations.
9+
error[E0738]: `#[track_caller]` may not be used on trait methods
1010
--> $DIR/error-with-trait-default-impl.rs:4:5
1111
|
1212
LL | #[track_caller]

src/test/ui/rfc-2091-track-caller/error-with-trait-fn-impl.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ trait Trait {
55
}
66

77
impl Trait for u64 {
8-
#[track_caller]
8+
#[track_caller] //~ ERROR: `#[track_caller]` may not be used on trait methods
99
fn unwrap(&self) {}
10-
//~^^ ERROR: `#[track_caller]` is not supported in traits yet.
1110
}
1211

1312
fn main() {}

src/test/ui/rfc-2091-track-caller/error-with-trait-fn-impl.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | #![feature(track_caller)]
66
|
77
= note: `#[warn(incomplete_features)]` on by default
88

9-
error[E0738]: `#[track_caller]` is not supported in traits yet.
9+
error[E0738]: `#[track_caller]` may not be used on trait methods
1010
--> $DIR/error-with-trait-fn-impl.rs:8:5
1111
|
1212
LL | #[track_caller]

0 commit comments

Comments
 (0)