Skip to content

Commit 6d3f1be

Browse files
committed
Auto merge of #100318 - Dylan-DPC:rollup-18tzp6q, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - #96478 (Implement `#[rustc_default_body_unstable]`) - #99787 (Rustdoc-Json: Document HRTB's on DynTrait) - #100181 (add method to get the mutability of an AllocId) - #100221 (Don't document impossible to call default trait items on impls) - #100228 (Don't ICE while suggesting updating item path.) - #100301 (Avoid `&str` to `String` conversions) - #100305 (Suggest adding an appropriate missing pattern excluding comments) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents cc4dd6f + 467e7aa commit 6d3f1be

File tree

41 files changed

+748
-128
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+748
-128
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4626,6 +4626,7 @@ dependencies = [
46264626
"rustc_attr",
46274627
"rustc_data_structures",
46284628
"rustc_errors",
4629+
"rustc_feature",
46294630
"rustc_graphviz",
46304631
"rustc_hir",
46314632
"rustc_hir_pretty",

compiler/rustc_ast_passes/src/feature_gate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
417417
|| attr.has_name(sym::stable)
418418
|| attr.has_name(sym::rustc_const_unstable)
419419
|| attr.has_name(sym::rustc_const_stable)
420+
|| attr.has_name(sym::rustc_default_body_unstable)
420421
{
421422
struct_span_err!(
422423
self.sess,

compiler/rustc_attr/src/builtin.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ impl ConstStability {
131131
}
132132
}
133133

134+
/// Represents the `#[rustc_default_body_unstable]` attribute.
135+
#[derive(Encodable, Decodable, Copy, Clone, Debug, PartialEq, Eq, Hash)]
136+
#[derive(HashStable_Generic)]
137+
pub struct DefaultBodyStability {
138+
pub level: StabilityLevel,
139+
pub feature: Symbol,
140+
}
141+
134142
/// The available stability levels.
135143
#[derive(Encodable, Decodable, PartialEq, Copy, Clone, Debug, Eq, Hash)]
136144
#[derive(HashStable_Generic)]
@@ -214,22 +222,24 @@ pub fn find_stability(
214222
sess: &Session,
215223
attrs: &[Attribute],
216224
item_sp: Span,
217-
) -> (Option<(Stability, Span)>, Option<(ConstStability, Span)>) {
225+
) -> (Option<(Stability, Span)>, Option<(ConstStability, Span)>, Option<(DefaultBodyStability, Span)>)
226+
{
218227
find_stability_generic(sess, attrs.iter(), item_sp)
219228
}
220229

221230
fn find_stability_generic<'a, I>(
222231
sess: &Session,
223232
attrs_iter: I,
224233
item_sp: Span,
225-
) -> (Option<(Stability, Span)>, Option<(ConstStability, Span)>)
234+
) -> (Option<(Stability, Span)>, Option<(ConstStability, Span)>, Option<(DefaultBodyStability, Span)>)
226235
where
227236
I: Iterator<Item = &'a Attribute>,
228237
{
229238
use StabilityLevel::*;
230239

231240
let mut stab: Option<(Stability, Span)> = None;
232241
let mut const_stab: Option<(ConstStability, Span)> = None;
242+
let mut body_stab: Option<(DefaultBodyStability, Span)> = None;
233243
let mut promotable = false;
234244
let mut allowed_through_unstable_modules = false;
235245

@@ -243,6 +253,7 @@ where
243253
sym::stable,
244254
sym::rustc_promotable,
245255
sym::rustc_allowed_through_unstable_modules,
256+
sym::rustc_default_body_unstable,
246257
]
247258
.iter()
248259
.any(|&s| attr.has_name(s))
@@ -280,7 +291,7 @@ where
280291

281292
let meta_name = meta.name_or_empty();
282293
match meta_name {
283-
sym::rustc_const_unstable | sym::unstable => {
294+
sym::rustc_const_unstable | sym::rustc_default_body_unstable | sym::unstable => {
284295
if meta_name == sym::unstable && stab.is_some() {
285296
handle_errors(
286297
&sess.parse_sess,
@@ -295,6 +306,13 @@ where
295306
AttrError::MultipleStabilityLevels,
296307
);
297308
break;
309+
} else if meta_name == sym::rustc_default_body_unstable && body_stab.is_some() {
310+
handle_errors(
311+
&sess.parse_sess,
312+
attr.span,
313+
AttrError::MultipleStabilityLevels,
314+
);
315+
break;
298316
}
299317

300318
let mut feature = None;
@@ -405,11 +423,16 @@ where
405423
};
406424
if sym::unstable == meta_name {
407425
stab = Some((Stability { level, feature }, attr.span));
408-
} else {
426+
} else if sym::rustc_const_unstable == meta_name {
409427
const_stab = Some((
410428
ConstStability { level, feature, promotable: false },
411429
attr.span,
412430
));
431+
} else if sym::rustc_default_body_unstable == meta_name {
432+
body_stab =
433+
Some((DefaultBodyStability { level, feature }, attr.span));
434+
} else {
435+
unreachable!("Unknown stability attribute {meta_name}");
413436
}
414437
}
415438
(None, _, _) => {
@@ -542,7 +565,7 @@ where
542565
}
543566
}
544567

545-
(stab, const_stab)
568+
(stab, const_stab, body_stab)
546569
}
547570

548571
pub fn find_crate_name(sess: &Session, attrs: &[Attribute]) -> Option<Symbol> {

compiler/rustc_const_eval/src/interpret/memory.rs

+7
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
520520

521521
/// Gives raw access to the `Allocation`, without bounds or alignment checks.
522522
/// The caller is responsible for calling the access hooks!
523+
///
524+
/// You almost certainly want to use `get_ptr_alloc`/`get_ptr_alloc_mut` instead.
523525
fn get_alloc_raw(
524526
&self,
525527
id: AllocId,
@@ -589,6 +591,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
589591
Ok(&self.get_alloc_raw(id)?.extra)
590592
}
591593

594+
/// Return the `mutability` field of the given allocation.
595+
pub fn get_alloc_mutability<'a>(&'a self, id: AllocId) -> InterpResult<'tcx, Mutability> {
596+
Ok(self.get_alloc_raw(id)?.mutability)
597+
}
598+
592599
/// Gives raw mutable access to the `Allocation`, without bounds or alignment checks.
593600
/// The caller is responsible for calling the access hooks!
594601
///

compiler/rustc_expand/src/base.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ impl SyntaxExtension {
772772
)
773773
})
774774
.unwrap_or_else(|| (None, helper_attrs));
775-
let (stability, const_stability) = attr::find_stability(&sess, attrs, span);
775+
let (stability, const_stability, body_stability) = attr::find_stability(&sess, attrs, span);
776776
if let Some((_, sp)) = const_stability {
777777
sess.parse_sess
778778
.span_diagnostic
@@ -784,6 +784,17 @@ impl SyntaxExtension {
784784
)
785785
.emit();
786786
}
787+
if let Some((_, sp)) = body_stability {
788+
sess.parse_sess
789+
.span_diagnostic
790+
.struct_span_err(sp, "macros cannot have body stability attributes")
791+
.span_label(sp, "invalid body stability attribute")
792+
.span_label(
793+
sess.source_map().guess_head_span(span),
794+
"body stability attribute affects this macro",
795+
)
796+
.emit();
797+
}
787798

788799
SyntaxExtension {
789800
kind,

compiler/rustc_feature/src/builtin_attrs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
499499
),
500500
ungated!(rustc_const_unstable, Normal, template!(List: r#"feature = "name""#), DuplicatesOk),
501501
ungated!(rustc_const_stable, Normal, template!(List: r#"feature = "name""#), DuplicatesOk),
502+
ungated!(
503+
rustc_default_body_unstable, Normal,
504+
template!(List: r#"feature = "name", reason = "...", issue = "N""#), DuplicatesOk
505+
),
502506
gated!(
503507
allow_internal_unstable, Normal, template!(Word, List: "feat1, feat2, ..."), DuplicatesOk,
504508
"allow_internal_unstable side-steps feature gating and stability checks",

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
207207
def_ident_span => { table }
208208
lookup_stability => { table }
209209
lookup_const_stability => { table }
210+
lookup_default_body_stability => { table }
210211
lookup_deprecation_entry => { table }
211212
visibility => { table }
212213
unused_generic_params => { table }

compiler/rustc_metadata/src/rmeta/encoder.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10291029
if should_encode_stability(def_kind) {
10301030
self.encode_stability(def_id);
10311031
self.encode_const_stability(def_id);
1032+
self.encode_default_body_stability(def_id);
10321033
self.encode_deprecation(def_id);
10331034
}
10341035
if should_encode_variances(def_kind) {
@@ -1385,6 +1386,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13851386
}
13861387
}
13871388

1389+
fn encode_default_body_stability(&mut self, def_id: DefId) {
1390+
debug!("EncodeContext::encode_default_body_stability({:?})", def_id);
1391+
1392+
// The query lookup can take a measurable amount of time in crates with many items. Check if
1393+
// the stability attributes are even enabled before using their queries.
1394+
if self.feat.staged_api || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked {
1395+
if let Some(stab) = self.tcx.lookup_default_body_stability(def_id) {
1396+
record!(self.tables.lookup_default_body_stability[def_id] <- stab)
1397+
}
1398+
}
1399+
}
1400+
13881401
fn encode_deprecation(&mut self, def_id: DefId) {
13891402
debug!("EncodeContext::encode_deprecation({:?})", def_id);
13901403
if let Some(depr) = self.tcx.lookup_deprecation(def_id) {

compiler/rustc_metadata/src/rmeta/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ define_tables! {
343343
def_ident_span: Table<DefIndex, LazyValue<Span>>,
344344
lookup_stability: Table<DefIndex, LazyValue<attr::Stability>>,
345345
lookup_const_stability: Table<DefIndex, LazyValue<attr::ConstStability>>,
346+
lookup_default_body_stability: Table<DefIndex, LazyValue<attr::DefaultBodyStability>>,
346347
lookup_deprecation_entry: Table<DefIndex, LazyValue<attr::Deprecation>>,
347348
// As an optimization, a missing entry indicates an empty `&[]`.
348349
explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Predicate<'static>, Span)>>,

compiler/rustc_middle/src/middle/stability.rs

+68-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub use self::StabilityLevel::*;
55

66
use crate::ty::{self, DefIdTree, TyCtxt};
77
use rustc_ast::NodeId;
8-
use rustc_attr::{self as attr, ConstStability, Deprecation, Stability};
8+
use rustc_attr::{self as attr, ConstStability, DefaultBodyStability, Deprecation, Stability};
99
use rustc_data_structures::fx::FxHashMap;
1010
use rustc_errors::{Applicability, Diagnostic};
1111
use rustc_feature::GateIssue;
@@ -61,6 +61,7 @@ pub struct Index {
6161
/// are filled by the annotator.
6262
pub stab_map: FxHashMap<LocalDefId, Stability>,
6363
pub const_stab_map: FxHashMap<LocalDefId, ConstStability>,
64+
pub default_body_stab_map: FxHashMap<LocalDefId, DefaultBodyStability>,
6465
pub depr_map: FxHashMap<LocalDefId, DeprecationEntry>,
6566
/// Mapping from feature name to feature name based on the `implied_by` field of `#[unstable]`
6667
/// attributes. If a `#[unstable(feature = "implier", implied_by = "impliee")]` attribute
@@ -86,6 +87,10 @@ impl Index {
8687
self.const_stab_map.get(&def_id).copied()
8788
}
8889

90+
pub fn local_default_body_stability(&self, def_id: LocalDefId) -> Option<DefaultBodyStability> {
91+
self.default_body_stab_map.get(&def_id).copied()
92+
}
93+
8994
pub fn local_deprecation_entry(&self, def_id: LocalDefId) -> Option<DeprecationEntry> {
9095
self.depr_map.get(&def_id).cloned()
9196
}
@@ -416,19 +421,19 @@ impl<'tcx> TyCtxt<'tcx> {
416421
return EvalResult::Allow;
417422
}
418423

424+
// Only the cross-crate scenario matters when checking unstable APIs
425+
let cross_crate = !def_id.is_local();
426+
if !cross_crate {
427+
return EvalResult::Allow;
428+
}
429+
419430
let stability = self.lookup_stability(def_id);
420431
debug!(
421432
"stability: \
422433
inspecting def_id={:?} span={:?} of stability={:?}",
423434
def_id, span, stability
424435
);
425436

426-
// Only the cross-crate scenario matters when checking unstable APIs
427-
let cross_crate = !def_id.is_local();
428-
if !cross_crate {
429-
return EvalResult::Allow;
430-
}
431-
432437
// Issue #38412: private items lack stability markers.
433438
if skip_stability_check_due_to_privacy(self, def_id) {
434439
return EvalResult::Allow;
@@ -492,6 +497,62 @@ impl<'tcx> TyCtxt<'tcx> {
492497
}
493498
}
494499

500+
/// Evaluates the default-impl stability of an item.
501+
///
502+
/// Returns `EvalResult::Allow` if the item's default implementation is stable, or unstable but the corresponding
503+
/// `#![feature]` has been provided. Returns `EvalResult::Deny` which describes the offending
504+
/// unstable feature otherwise.
505+
pub fn eval_default_body_stability(self, def_id: DefId, span: Span) -> EvalResult {
506+
let is_staged_api = self.lookup_stability(def_id.krate.as_def_id()).is_some();
507+
if !is_staged_api {
508+
return EvalResult::Allow;
509+
}
510+
511+
// Only the cross-crate scenario matters when checking unstable APIs
512+
let cross_crate = !def_id.is_local();
513+
if !cross_crate {
514+
return EvalResult::Allow;
515+
}
516+
517+
let stability = self.lookup_default_body_stability(def_id);
518+
debug!(
519+
"body stability: inspecting def_id={def_id:?} span={span:?} of stability={stability:?}"
520+
);
521+
522+
// Issue #38412: private items lack stability markers.
523+
if skip_stability_check_due_to_privacy(self, def_id) {
524+
return EvalResult::Allow;
525+
}
526+
527+
match stability {
528+
Some(DefaultBodyStability {
529+
level: attr::Unstable { reason, issue, is_soft, .. },
530+
feature,
531+
}) => {
532+
if span.allows_unstable(feature) {
533+
debug!("body stability: skipping span={:?} since it is internal", span);
534+
return EvalResult::Allow;
535+
}
536+
if self.features().active(feature) {
537+
return EvalResult::Allow;
538+
}
539+
540+
EvalResult::Deny {
541+
feature,
542+
reason: reason.to_opt_reason(),
543+
issue,
544+
suggestion: None,
545+
is_soft,
546+
}
547+
}
548+
Some(_) => {
549+
// Stable APIs are always ok to call
550+
EvalResult::Allow
551+
}
552+
None => EvalResult::Unmarked,
553+
}
554+
}
555+
495556
/// Checks if an item is stable or error out.
496557
///
497558
/// If the item defined by `def_id` is unstable and the corresponding `#![feature]` does not

compiler/rustc_middle/src/query/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,11 @@ rustc_queries! {
10941094
separate_provide_extern
10951095
}
10961096

1097+
query lookup_default_body_stability(def_id: DefId) -> Option<attr::DefaultBodyStability> {
1098+
desc { |tcx| "looking up default body stability of `{}`", tcx.def_path_str(def_id) }
1099+
separate_provide_extern
1100+
}
1101+
10971102
query should_inherit_track_caller(def_id: DefId) -> bool {
10981103
desc { |tcx| "computing should_inherit_track_caller of `{}`", tcx.def_path_str(def_id) }
10991104
}
@@ -1951,6 +1956,14 @@ rustc_queries! {
19511956
}
19521957
}
19531958

1959+
query is_impossible_method(key: (DefId, DefId)) -> bool {
1960+
desc { |tcx|
1961+
"checking if {} is impossible to call within {}",
1962+
tcx.def_path_str(key.1),
1963+
tcx.def_path_str(key.0),
1964+
}
1965+
}
1966+
19541967
query method_autoderef_steps(
19551968
goal: CanonicalTyGoal<'tcx>
19561969
) -> MethodAutoderefStepsResult<'tcx> {

compiler/rustc_middle/src/ty/parameterized.rs

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ trivially_parameterized_over_tcx! {
6464
rustc_ast::Attribute,
6565
rustc_ast::MacArgs,
6666
rustc_attr::ConstStability,
67+
rustc_attr::DefaultBodyStability,
6768
rustc_attr::Deprecation,
6869
rustc_attr::Stability,
6970
rustc_hir::Constness,

0 commit comments

Comments
 (0)