Skip to content

Commit 275cf4b

Browse files
committed
Auto merge of #65229 - Centril:rollup-wnr46vg, r=Centril
Rollup of 4 pull requests Successful merges: - #64656 (Implement (HashMap) Entry::insert as per #60142) - #65037 (`#[track_caller]` feature gate (RFC 2091 1/N)) - #65166 (Suggest to add `move` keyword for generator capture) - #65175 (add more info in debug traces for gcu merging) Failed merges: r? @ghost
2 parents b5bd31e + e27f029 commit 275cf4b

37 files changed

+542
-10
lines changed

Cargo.lock

+18-3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ dependencies = [
107107
"winapi 0.3.6",
108108
]
109109

110+
[[package]]
111+
name = "autocfg"
112+
version = "0.1.6"
113+
source = "registry+https://github.com/rust-lang/crates.io-index"
114+
checksum = "b671c8fb71b457dd4ae18c4ba1e59aa81793daacc361d82fcd410cef0d491875"
115+
110116
[[package]]
111117
name = "backtrace"
112118
version = "0.3.37"
@@ -1269,7 +1275,7 @@ version = "2.0.1"
12691275
source = "registry+https://github.com/rust-lang/crates.io-index"
12701276
checksum = "df044dd42cdb7e32f28557b661406fc0f2494be75199779998810dbc35030e0d"
12711277
dependencies = [
1272-
"hashbrown",
1278+
"hashbrown 0.5.0",
12731279
"lazy_static 1.3.0",
12741280
"log",
12751281
"pest",
@@ -1286,10 +1292,19 @@ version = "0.5.0"
12861292
source = "registry+https://github.com/rust-lang/crates.io-index"
12871293
checksum = "e1de41fb8dba9714efd92241565cdff73f78508c95697dd56787d3cba27e2353"
12881294
dependencies = [
1295+
"serde",
1296+
]
1297+
1298+
[[package]]
1299+
name = "hashbrown"
1300+
version = "0.6.1"
1301+
source = "registry+https://github.com/rust-lang/crates.io-index"
1302+
checksum = "6587d09be37fb98a11cb08b9000a3f592451c1b1b613ca69d949160e313a430a"
1303+
dependencies = [
1304+
"autocfg",
12891305
"compiler_builtins",
12901306
"rustc-std-workspace-alloc",
12911307
"rustc-std-workspace-core",
1292-
"serde",
12931308
]
12941309

12951310
[[package]]
@@ -4109,7 +4124,7 @@ dependencies = [
41094124
"core",
41104125
"dlmalloc",
41114126
"fortanix-sgx-abi",
4112-
"hashbrown",
4127+
"hashbrown 0.6.1",
41134128
"libc",
41144129
"panic_abort",
41154130
"panic_unwind",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# `track_caller`
2+
3+
The tracking issue for this feature is: [#47809](https://github.com/rust-lang/rust/issues/47809).
4+
5+
------------------------

src/librustc/error_codes.rs

+20
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,25 @@ These attributes are meant to only be used by the standard library and are
21202120
rejected in your own crates.
21212121
"##,
21222122

2123+
E0736: r##"
2124+
#[track_caller] and #[naked] cannot be applied to the same function.
2125+
2126+
Erroneous code example:
2127+
2128+
```compile_fail,E0736
2129+
#![feature(track_caller)]
2130+
2131+
#[naked]
2132+
#[track_caller]
2133+
fn foo() {}
2134+
```
2135+
2136+
This is primarily due to ABI incompatibilities between the two attributes.
2137+
See [RFC 2091] for details on this and other limitations.
2138+
2139+
[RFC 2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md
2140+
"##,
2141+
21232142
;
21242143
// E0006, // merged with E0005
21252144
// E0101, // replaced with E0282
@@ -2179,4 +2198,5 @@ rejected in your own crates.
21792198
E0726, // non-explicit (not `'_`) elided lifetime in unsupported position
21802199
E0727, // `async` generators are not yet supported
21812200
E0728, // `await` must be in an `async` function or block
2201+
E0739, // invalid track_caller application/syntax
21822202
}

src/librustc/hir/check_attr.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::ty::TyCtxt;
1111
use crate::ty::query::Providers;
1212

1313
use std::fmt::{self, Display};
14-
use syntax::symbol::sym;
14+
use syntax::{attr, symbol::sym};
1515
use syntax_pos::Span;
1616

1717
#[derive(Copy, Clone, PartialEq)]
@@ -103,6 +103,8 @@ impl CheckAttrVisitor<'tcx> {
103103
self.check_marker(attr, item, target)
104104
} else if attr.check_name(sym::target_feature) {
105105
self.check_target_feature(attr, item, target)
106+
} else if attr.check_name(sym::track_caller) {
107+
self.check_track_caller(attr, &item, target)
106108
} else {
107109
true
108110
};
@@ -135,6 +137,32 @@ impl CheckAttrVisitor<'tcx> {
135137
}
136138
}
137139

140+
/// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid.
141+
fn check_track_caller(&self, attr: &hir::Attribute, item: &hir::Item, target: Target) -> bool {
142+
if target != Target::Fn {
143+
struct_span_err!(
144+
self.tcx.sess,
145+
attr.span,
146+
E0739,
147+
"attribute should be applied to function"
148+
)
149+
.span_label(item.span, "not a function")
150+
.emit();
151+
false
152+
} else if attr::contains_name(&item.attrs, sym::naked) {
153+
struct_span_err!(
154+
self.tcx.sess,
155+
attr.span,
156+
E0736,
157+
"cannot use `#[track_caller]` with `#[naked]`",
158+
)
159+
.emit();
160+
false
161+
} else {
162+
true
163+
}
164+
}
165+
138166
/// Checks if the `#[non_exhaustive]` attribute on an `item` is valid. Returns `true` if valid.
139167
fn check_non_exhaustive(
140168
&self,

src/librustc/hir/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2734,7 +2734,9 @@ bitflags! {
27342734
const USED = 1 << 9;
27352735
/// #[ffi_returns_twice], indicates that an extern function can return
27362736
/// multiple times
2737-
const FFI_RETURNS_TWICE = 1 << 10;
2737+
const FFI_RETURNS_TWICE = 1 << 10;
2738+
/// #[track_caller]: allow access to the caller location
2739+
const TRACK_CALLER = 1 << 11;
27382740
}
27392741
}
27402742

src/librustc_mir/borrow_check/conflict_errors.rs

+26
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
750750
let kind_place = kind.filter(|_| place_desc.is_some()).map(|k| (k, place_span.0));
751751
let explanation = self.explain_why_borrow_contains_point(location, &borrow, kind_place);
752752

753+
debug!(
754+
"report_borrowed_value_does_not_live_long_enough(place_desc: {:?}, explanation: {:?})",
755+
place_desc,
756+
explanation
757+
);
753758
let err = match (place_desc, explanation) {
754759
(Some(_), _) if self.is_place_thread_local(root_place) => {
755760
self.report_thread_local_value_does_not_live_long_enough(drop_span, borrow_span)
@@ -790,6 +795,24 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
790795
span,
791796
&format!("`{}`", name),
792797
),
798+
(
799+
Some(ref name),
800+
BorrowExplanation::MustBeValidFor {
801+
category: category @ ConstraintCategory::OpaqueType,
802+
from_closure: false,
803+
ref region_name,
804+
span,
805+
..
806+
},
807+
808+
) if borrow_spans.for_generator() => self.report_escaping_closure_capture(
809+
borrow_spans.args_or_use(),
810+
borrow_span,
811+
region_name,
812+
category,
813+
span,
814+
&format!("`{}`", name),
815+
),
793816
(
794817
ref name,
795818
BorrowExplanation::MustBeValidFor {
@@ -1214,6 +1237,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12141237
ConstraintCategory::Return => {
12151238
err.span_note(constraint_span, "closure is returned here");
12161239
}
1240+
ConstraintCategory::OpaqueType => {
1241+
err.span_note(constraint_span, "generator is returned here");
1242+
}
12171243
ConstraintCategory::CallArgument => {
12181244
fr_name.highlight_region_name(&mut err);
12191245
err.span_note(

src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use syntax_pos::Span;
1717

1818
mod find_use;
1919

20+
#[derive(Debug)]
2021
pub(in crate::borrow_check) enum BorrowExplanation {
2122
UsedLater(LaterUseKind, Span),
2223
UsedLaterInLoop(LaterUseKind, Span),
@@ -35,7 +36,7 @@ pub(in crate::borrow_check) enum BorrowExplanation {
3536
Unexplained,
3637
}
3738

38-
#[derive(Clone, Copy)]
39+
#[derive(Clone, Copy, Debug)]
3940
pub(in crate::borrow_check) enum LaterUseKind {
4041
TraitCapture,
4142
ClosureCapture,

src/librustc_mir/monomorphize/partitioning.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,9 @@ fn merge_codegen_units<'tcx>(
494494
for (k, v) in smallest.items_mut().drain() {
495495
second_smallest.items_mut().insert(k, v);
496496
}
497+
debug!("CodegenUnit {} merged in to CodegenUnit {}",
498+
smallest.name(),
499+
second_smallest.name());
497500
}
498501

499502
let cgu_name_builder = &mut CodegenUnitNameBuilder::new(tcx);
@@ -774,18 +777,19 @@ where
774777
if cfg!(debug_assertions) {
775778
debug!("{}", label);
776779
for cgu in cgus {
777-
debug!("CodegenUnit {}:", cgu.name());
780+
debug!("CodegenUnit {} estimated size {} :", cgu.name(), cgu.size_estimate());
778781

779782
for (mono_item, linkage) in cgu.items() {
780783
let symbol_name = mono_item.symbol_name(tcx).name.as_str();
781784
let symbol_hash_start = symbol_name.rfind('h');
782785
let symbol_hash = symbol_hash_start.map(|i| &symbol_name[i ..])
783786
.unwrap_or("<no hash>");
784787

785-
debug!(" - {} [{:?}] [{}]",
788+
debug!(" - {} [{:?}] [{}] estimated size {}",
786789
mono_item.to_string(tcx, true),
787790
linkage,
788-
symbol_hash);
791+
symbol_hash,
792+
mono_item.size_estimate(tcx));
789793
}
790794

791795
debug!("");

src/librustc_typeck/check/wfcheck.rs

+36
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ 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+
}
175187
}
176188

177189
pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: DefId) {
@@ -182,6 +194,30 @@ pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: DefId) {
182194
hir::ImplItemKind::Method(ref sig, _) => Some(sig),
183195
_ => None
184196
};
197+
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+
185221
check_associated_item(tcx, impl_item.hir_id, impl_item.span, method_sig);
186222
}
187223

src/librustc_typeck/collect.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2594,6 +2594,16 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
25942594
codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED;
25952595
} else if attr.check_name(sym::thread_local) {
25962596
codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL;
2597+
} else if attr.check_name(sym::track_caller) {
2598+
if tcx.fn_sig(id).abi() != abi::Abi::Rust {
2599+
struct_span_err!(
2600+
tcx.sess,
2601+
attr.span,
2602+
E0737,
2603+
"rust ABI is required to use `#[track_caller]`"
2604+
).emit();
2605+
}
2606+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
25972607
} else if attr.check_name(sym::export_name) {
25982608
if let Some(s) = attr.value_str() {
25992609
if s.as_str().contains("\0") {

src/librustc_typeck/error_codes.rs

+69
Original file line numberDiff line numberDiff line change
@@ -4905,6 +4905,75 @@ fn foo_recursive(n: usize) -> Pin<Box<dyn Future<Output = ()>>> {
49054905
The `Box<...>` ensures that the result is of known size,
49064906
and the pin is required to keep it in the same place in memory.
49074907
"##,
4908+
4909+
E0737: r##"
4910+
#[track_caller] requires functions to have the "Rust" ABI for implicitly
4911+
receiving caller location. See [RFC 2091] for details on this and other
4912+
restrictions.
4913+
4914+
Erroneous code example:
4915+
4916+
```compile_fail,E0737
4917+
#![feature(track_caller)]
4918+
4919+
#[track_caller]
4920+
extern "C" fn foo() {}
4921+
```
4922+
4923+
[RFC 2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md
4924+
"##,
4925+
4926+
E0738: r##"
4927+
#[track_caller] cannot be used in traits yet. This is due to limitations in the
4928+
compiler which are likely to be temporary. See [RFC 2091] for details on this
4929+
and other restrictions.
4930+
4931+
Erroneous example with a trait method implementation:
4932+
4933+
```compile_fail,E0738
4934+
#![feature(track_caller)]
4935+
4936+
trait Foo {
4937+
fn bar(&self);
4938+
}
4939+
4940+
impl Foo for u64 {
4941+
#[track_caller]
4942+
fn bar(&self) {}
4943+
}
4944+
```
4945+
4946+
Erroneous example with a blanket trait method implementation:
4947+
4948+
```compile_fail,E0738
4949+
#![feature(track_caller)]
4950+
4951+
trait Foo {
4952+
#[track_caller]
4953+
fn bar(&self) {}
4954+
fn baz(&self);
4955+
}
4956+
```
4957+
4958+
Erroneous example with a trait method declaration:
4959+
4960+
```compile_fail,E0738
4961+
#![feature(track_caller)]
4962+
4963+
trait Foo {
4964+
fn bar(&self) {}
4965+
4966+
#[track_caller]
4967+
fn baz(&self);
4968+
}
4969+
```
4970+
4971+
Note that while the compiler may be able to support the attribute in traits in
4972+
the future, [RFC 2091] prohibits their implementation without a follow-up RFC.
4973+
4974+
[RFC 2091]: https://github.com/rust-lang/rfcs/blob/master/text/2091-inline-semantic.md
4975+
"##,
4976+
49084977
;
49094978
// E0035, merged into E0087/E0089
49104979
// E0036, merged into E0087/E0089

0 commit comments

Comments
 (0)