Skip to content

Commit 2643b16

Browse files
committed
Auto merge of rust-lang#99816 - GuillaumeGomez:rollup-tyobksa, r=GuillaumeGomez
Rollup of 7 pull requests Successful merges: - rust-lang#94247 (Fix slice::ChunksMut aliasing) - rust-lang#99358 (Allow `ValTree::try_to_raw_bytes` on `u8` array) - rust-lang#99651 (Deeply deny fn and raw ptrs in const generics) - rust-lang#99710 (lint: add bad opt access internal lint) - rust-lang#99717 (Add some comments to the docs issue template to clarify) - rust-lang#99728 (Clean up HIR-based lifetime resolution) - rust-lang#99812 (Fix headings colors) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents da5b546 + 2d52aa0 commit 2643b16

File tree

66 files changed

+1570
-1350
lines changed

Some content is hidden

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

66 files changed

+1570
-1350
lines changed

.github/ISSUE_TEMPLATE/documentation.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@ about: Create a report for a documentation problem.
44
labels: A-docs
55
---
66
<!--
7+
78
Thank you for finding a documentation problem! 📚
89
910
Documentation problems might be grammatical issues, typos, or unclear wording, please provide details regarding the documentation including where it is present.
1011
12+
Note: If your issue is for one of these, please use their dedicated issue tracker instead:
13+
14+
- The Rust Book: https://github.com/rust-lang/book/issues
15+
- Rust by Example: https://github.com/rust-lang/rust-by-example/issues
16+
- The Edition Guide: https://github.com/rust-lang/edition-guide/issues
17+
- The Cargo Book: https://github.com/rust-lang/cargo/issues
18+
- The Clippy Book: https://github.com/rust-lang/rust-clippy/issues
19+
- The Reference: https://github.com/rust-lang/reference/issues
20+
- The Rustonomicon: https://github.com/rust-lang/nomicon/issues
21+
- The Embedded Book: https://github.com/rust-embedded/book/issues
22+
23+
All other documentation issues should be filed here.
24+
25+
Or, if you find an issue related to rustdoc (e.g. doctest, rustdoc UI), please use the bug report or blank issue template instead.
26+
1127
-->
1228

1329
### Location
1430

1531
### Summary
16-

compiler/rustc_ast_lowering/src/lib.rs

+1-23
Original file line numberDiff line numberDiff line change
@@ -1883,29 +1883,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18831883
}
18841884
hir::LifetimeName::Param(param, ParamName::Fresh)
18851885
}
1886-
LifetimeRes::Anonymous { binder, elided } => {
1887-
let mut l_name = None;
1888-
if let Some(mut captured_lifetimes) = self.captured_lifetimes.take() {
1889-
if !captured_lifetimes.binders_to_ignore.contains(&binder) {
1890-
let p_id = self.next_node_id();
1891-
let p_def_id = self.create_def(
1892-
captured_lifetimes.parent_def_id,
1893-
p_id,
1894-
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
1895-
);
1896-
captured_lifetimes
1897-
.captures
1898-
.insert(p_def_id, (span, p_id, ParamName::Fresh, res));
1899-
l_name = Some(hir::LifetimeName::Param(p_def_id, ParamName::Fresh));
1900-
}
1901-
self.captured_lifetimes = Some(captured_lifetimes);
1902-
};
1903-
l_name.unwrap_or(if elided {
1904-
hir::LifetimeName::Implicit
1905-
} else {
1906-
hir::LifetimeName::Underscore
1907-
})
1908-
}
1886+
LifetimeRes::Infer => hir::LifetimeName::Infer,
19091887
LifetimeRes::Static => hir::LifetimeName::Static,
19101888
LifetimeRes::Error => hir::LifetimeName::Error,
19111889
res => panic!("Unexpected lifetime resolution {:?} for {:?} at {:?}", res, ident, span),

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
589589

590590
hir::LifetimeName::Param(_, hir::ParamName::Fresh)
591591
| hir::LifetimeName::ImplicitObjectLifetimeDefault
592-
| hir::LifetimeName::Implicit
593-
| hir::LifetimeName::Underscore => {
592+
| hir::LifetimeName::Infer => {
594593
// In this case, the user left off the lifetime; so
595594
// they wrote something like:
596595
//

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl Qualif for CustomEq {
226226
// because that component may be part of an enum variant (e.g.,
227227
// `Option::<NonStructuralMatchTy>::Some`), in which case some values of this type may be
228228
// structural-match (`Option::None`).
229-
traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty, true).is_some()
229+
traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty).is_some()
230230
}
231231

232232
fn in_adt_inherently<'tcx>(

compiler/rustc_driver/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ impl Callbacks for TimePassesCallbacks {
123123
fn config(&mut self, config: &mut interface::Config) {
124124
// If a --prints=... option has been given, we don't print the "total"
125125
// time because it will mess up the --prints output. See #64339.
126-
self.time_passes = config.opts.prints.is_empty()
127-
&& (config.opts.unstable_opts.time_passes || config.opts.unstable_opts.time);
126+
self.time_passes = config.opts.prints.is_empty() && config.opts.time_passes();
128127
config.opts.trimmed_def_paths = TrimmedDefPaths::GoodPath;
129128
}
130129
}
@@ -249,7 +248,7 @@ fn run_compiler(
249248
if sopts.describe_lints {
250249
let mut lint_store = rustc_lint::new_lint_store(
251250
sopts.unstable_opts.no_interleave_lints,
252-
compiler.session().unstable_options(),
251+
compiler.session().enable_internal_lints(),
253252
);
254253
let registered_lints =
255254
if let Some(register_lints) = compiler.register_lints() {

compiler/rustc_error_messages/locales/en-US/passes.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,9 @@ passes-unused-duplicate = unused attribute
256256
passes-unused-multiple = multiple `{$name}` attributes
257257
.suggestion = remove this attribute
258258
.note = attribute also specified here
259+
260+
passes-rustc-lint-opt-ty = `#[rustc_lint_opt_ty]` should be applied to a struct
261+
.label = not a struct
262+
263+
passes-rustc-lint-opt-deny-field-access = `#[rustc_lint_opt_deny_field_access]` should be applied to a field
264+
.label = not a field

compiler/rustc_feature/src/builtin_attrs.rs

+6
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,12 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
619619
// Used by the `rustc::untranslatable_diagnostic` and `rustc::diagnostic_outside_of_impl` lints
620620
// to assist in changes to diagnostic APIs.
621621
rustc_attr!(rustc_lint_diagnostics, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
622+
// Used by the `rustc::bad_opt_access` lint to identify `DebuggingOptions` and `CodegenOptions`
623+
// types (as well as any others in future).
624+
rustc_attr!(rustc_lint_opt_ty, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
625+
// Used by the `rustc::bad_opt_access` lint on fields
626+
// types (as well as any others in future).
627+
rustc_attr!(rustc_lint_opt_deny_field_access, Normal, template!(List: "message"), WarnFollowing, INTERNAL_UNSTABLE),
622628

623629
// ==========================================================================
624630
// Internal attributes, Const related:

compiler/rustc_hir/src/def.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -738,13 +738,8 @@ pub enum LifetimeRes {
738738
binder: NodeId,
739739
},
740740
/// This variant is used for anonymous lifetimes that we did not resolve during
741-
/// late resolution. Shifting the work to the HIR lifetime resolver.
742-
Anonymous {
743-
/// Id of the introducing place. See `Param`.
744-
binder: NodeId,
745-
/// Whether this lifetime was spelled or elided.
746-
elided: bool,
747-
},
741+
/// late resolution. Those lifetimes will be inferred by typechecking.
742+
Infer,
748743
/// Explicit `'static` lifetime.
749744
Static,
750745
/// Resolution failure.

compiler/rustc_hir/src/hir.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ pub enum LifetimeName {
9090
/// User-given names or fresh (synthetic) names.
9191
Param(LocalDefId, ParamName),
9292

93-
/// User wrote nothing (e.g., the lifetime in `&u32`).
94-
Implicit,
95-
9693
/// Implicit lifetime in a context like `dyn Foo`. This is
9794
/// distinguished from implicit lifetimes elsewhere because the
9895
/// lifetime that they default to must appear elsewhere within the
@@ -110,8 +107,9 @@ pub enum LifetimeName {
110107
/// that was already reported.
111108
Error,
112109

113-
/// User wrote specifies `'_`.
114-
Underscore,
110+
/// User wrote an anonymous lifetime, either `'_` or nothing.
111+
/// The semantics of this lifetime should be inferred by typechecking code.
112+
Infer,
115113

116114
/// User wrote `'static`.
117115
Static,
@@ -120,10 +118,8 @@ pub enum LifetimeName {
120118
impl LifetimeName {
121119
pub fn ident(&self) -> Ident {
122120
match *self {
123-
LifetimeName::ImplicitObjectLifetimeDefault
124-
| LifetimeName::Implicit
125-
| LifetimeName::Error => Ident::empty(),
126-
LifetimeName::Underscore => Ident::with_dummy_span(kw::UnderscoreLifetime),
121+
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Error => Ident::empty(),
122+
LifetimeName::Infer => Ident::with_dummy_span(kw::UnderscoreLifetime),
127123
LifetimeName::Static => Ident::with_dummy_span(kw::StaticLifetime),
128124
LifetimeName::Param(_, param_name) => param_name.ident(),
129125
}
@@ -132,8 +128,7 @@ impl LifetimeName {
132128
pub fn is_anonymous(&self) -> bool {
133129
match *self {
134130
LifetimeName::ImplicitObjectLifetimeDefault
135-
| LifetimeName::Implicit
136-
| LifetimeName::Underscore
131+
| LifetimeName::Infer
137132
| LifetimeName::Param(_, ParamName::Fresh)
138133
| LifetimeName::Error => true,
139134
LifetimeName::Static | LifetimeName::Param(..) => false,
@@ -142,9 +137,7 @@ impl LifetimeName {
142137

143138
pub fn is_elided(&self) -> bool {
144139
match self {
145-
LifetimeName::ImplicitObjectLifetimeDefault
146-
| LifetimeName::Implicit
147-
| LifetimeName::Underscore => true,
140+
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Infer => true,
148141

149142
// It might seem surprising that `Fresh` counts as
150143
// *not* elided -- but this is because, as far as the code

compiler/rustc_hir/src/intravisit.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,8 @@ pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime
496496
| LifetimeName::Param(_, ParamName::Error)
497497
| LifetimeName::Static
498498
| LifetimeName::Error
499-
| LifetimeName::Implicit
500499
| LifetimeName::ImplicitObjectLifetimeDefault
501-
| LifetimeName::Underscore => {}
500+
| LifetimeName::Infer => {}
502501
}
503502
}
504503

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/find_anon_type.rs

+1-30
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,6 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
100100
// the lifetime of the TyRptr
101101
let hir_id = lifetime.hir_id;
102102
match (self.tcx.named_region(hir_id), self.bound_region) {
103-
// Find the index of the anonymous region that was part of the
104-
// error. We will then search the function parameters for a bound
105-
// region at the right depth with the same index
106-
(
107-
Some(rl::Region::LateBoundAnon(debruijn_index, _, anon_index)),
108-
ty::BrAnon(br_index),
109-
) => {
110-
debug!(
111-
"LateBoundAnon depth = {:?} anon_index = {:?} br_index={:?}",
112-
debruijn_index, anon_index, br_index
113-
);
114-
if debruijn_index == self.current_index && anon_index == br_index {
115-
self.found_type = Some(arg);
116-
return; // we can stop visiting now
117-
}
118-
}
119-
120103
// Find the index of the named region that was part of the
121104
// error. We will then search the function parameters for a bound
122105
// region at the right depth with the same index
@@ -151,8 +134,7 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
151134
rl::Region::Static
152135
| rl::Region::Free(_, _)
153136
| rl::Region::EarlyBound(_, _)
154-
| rl::Region::LateBound(_, _, _)
155-
| rl::Region::LateBoundAnon(_, _, _),
137+
| rl::Region::LateBound(_, _, _),
156138
)
157139
| None,
158140
_,
@@ -206,16 +188,6 @@ impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
206188
fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
207189
match (self.tcx.named_region(lifetime.hir_id), self.bound_region) {
208190
// the lifetime of the TyPath!
209-
(
210-
Some(rl::Region::LateBoundAnon(debruijn_index, _, anon_index)),
211-
ty::BrAnon(br_index),
212-
) => {
213-
if debruijn_index == self.current_index && anon_index == br_index {
214-
self.found_it = true;
215-
return;
216-
}
217-
}
218-
219191
(Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => {
220192
debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
221193
if id == def_id {
@@ -239,7 +211,6 @@ impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
239211
rl::Region::Static
240212
| rl::Region::EarlyBound(_, _)
241213
| rl::Region::LateBound(_, _, _)
242-
| rl::Region::LateBoundAnon(_, _, _)
243214
| rl::Region::Free(_, _),
244215
)
245216
| None,

compiler/rustc_interface/src/interface.rs

+2
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R
329329
})
330330
}
331331

332+
// JUSTIFICATION: before session exists, only config
333+
#[cfg_attr(not(bootstrap), allow(rustc::bad_opt_access))]
332334
pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
333335
tracing::trace!("run_compiler");
334336
util::run_in_thread_pool_with_globals(

compiler/rustc_interface/src/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub fn register_plugins<'a>(
210210

211211
let mut lint_store = rustc_lint::new_lint_store(
212212
sess.opts.unstable_opts.no_interleave_lints,
213-
sess.unstable_options(),
213+
sess.enable_internal_lints(),
214214
);
215215
register_lints(sess, &mut lint_store);
216216

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg_attr(not(bootstrap), allow(rustc::bad_opt_access))]
12
use crate::interface::parse_cfgspecs;
23

34
use rustc_data_structures::fx::FxHashSet;

compiler/rustc_interface/src/util.rs

+2
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<C
559559
// Only check command line flags if present. If no types are specified by
560560
// command line, then reuse the empty `base` Vec to hold the types that
561561
// will be found in crate attributes.
562+
// JUSTIFICATION: before wrapper fn is available
563+
#[cfg_attr(not(bootstrap), allow(rustc::bad_opt_access))]
562564
let mut base = session.opts.crate_types.clone();
563565
if base.is_empty() {
564566
base.extend(attr_types);

compiler/rustc_lint/src/internal.rs

+35-14
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,6 @@ fn typeck_results_of_method_fn<'tcx>(
5151
cx: &LateContext<'tcx>,
5252
expr: &Expr<'_>,
5353
) -> Option<(Span, DefId, ty::subst::SubstsRef<'tcx>)> {
54-
// FIXME(rustdoc): Lints which use this function use typecheck results which can cause
55-
// `rustdoc` to error if there are resolution failures.
56-
//
57-
// As internal lints are currently always run if there are `unstable_options`, they are added
58-
// to the lint store of rustdoc. Internal lints are also not used via the `lint_mod` query.
59-
// Crate lints run outside of a query so rustdoc currently doesn't disable them.
60-
//
61-
// Instead of relying on this, either change crate lints to a query disabled by rustdoc, only
62-
// run internal lints if the user is explicitly opting in or figure out a different way to
63-
// avoid running lints for rustdoc.
64-
if cx.tcx.sess.opts.actually_rustdoc {
65-
return None;
66-
}
67-
6854
match expr.kind {
6955
ExprKind::MethodCall(segment, _, _)
7056
if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) =>
@@ -446,3 +432,38 @@ impl LateLintPass<'_> for Diagnostics {
446432
}
447433
}
448434
}
435+
436+
declare_tool_lint! {
437+
pub rustc::BAD_OPT_ACCESS,
438+
Deny,
439+
"prevent using options by field access when there is a wrapper function",
440+
report_in_external_macro: true
441+
}
442+
443+
declare_lint_pass!(BadOptAccess => [ BAD_OPT_ACCESS ]);
444+
445+
impl LateLintPass<'_> for BadOptAccess {
446+
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
447+
let ExprKind::Field(base, target) = expr.kind else { return };
448+
let Some(adt_def) = cx.typeck_results().expr_ty(base).ty_adt_def() else { return };
449+
// Skip types without `#[rustc_lint_opt_ty]` - only so that the rest of the lint can be
450+
// avoided.
451+
if !cx.tcx.has_attr(adt_def.did(), sym::rustc_lint_opt_ty) {
452+
return;
453+
}
454+
455+
for field in adt_def.all_fields() {
456+
if field.name == target.name &&
457+
let Some(attr) = cx.tcx.get_attr(field.did, sym::rustc_lint_opt_deny_field_access) &&
458+
let Some(items) = attr.meta_item_list() &&
459+
let Some(item) = items.first() &&
460+
let Some(literal) = item.literal() &&
461+
let ast::LitKind::Str(val, _) = literal.kind
462+
{
463+
cx.struct_span_lint(BAD_OPT_ACCESS, expr.span, |lint| {
464+
lint.build(val.as_str()).emit(); }
465+
);
466+
}
467+
}
468+
}
469+
}

compiler/rustc_lint/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,14 @@ fn register_internals(store: &mut LintStore) {
509509
store.register_late_pass(|| Box::new(TyTyKind));
510510
store.register_lints(&Diagnostics::get_lints());
511511
store.register_late_pass(|| Box::new(Diagnostics));
512+
store.register_lints(&BadOptAccess::get_lints());
513+
store.register_late_pass(|| Box::new(BadOptAccess));
512514
store.register_lints(&PassByValue::get_lints());
513515
store.register_late_pass(|| Box::new(PassByValue));
516+
// FIXME(davidtwco): deliberately do not include `UNTRANSLATABLE_DIAGNOSTIC` and
517+
// `DIAGNOSTIC_OUTSIDE_OF_IMPL` here because `-Wrustc::internal` is provided to every crate and
518+
// these lints will trigger all of the time - change this once migration to diagnostic structs
519+
// and translation is completed
514520
store.register_group(
515521
false,
516522
"rustc::internal",
@@ -523,6 +529,7 @@ fn register_internals(store: &mut LintStore) {
523529
LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
524530
LintId::of(USAGE_OF_QUALIFIED_TY),
525531
LintId::of(EXISTING_DOC_KEYWORD),
532+
LintId::of(BAD_OPT_ACCESS),
526533
],
527534
);
528535
}

compiler/rustc_middle/src/middle/resolve_lifetime.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub enum Region {
1212
Static,
1313
EarlyBound(/* index */ u32, /* lifetime decl */ DefId),
1414
LateBound(ty::DebruijnIndex, /* late-bound index */ u32, /* lifetime decl */ DefId),
15-
LateBoundAnon(ty::DebruijnIndex, /* late-bound index */ u32, /* anon index */ u32),
1615
Free(DefId, /* lifetime decl */ DefId),
1716
}
1817

compiler/rustc_middle/src/ty/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<'tcx> Const<'tcx> {
203203
pub fn to_valtree(self) -> ty::ValTree<'tcx> {
204204
match self.kind() {
205205
ty::ConstKind::Value(valtree) => valtree,
206-
_ => bug!("expected ConstKind::Value"),
206+
_ => bug!("expected ConstKind::Value, got {:?}", self.kind()),
207207
}
208208
}
209209

0 commit comments

Comments
 (0)