Skip to content

Commit 712d962

Browse files
committed
Auto merge of rust-lang#115104 - compiler-errors:rollup-8235xz5, r=compiler-errors
Rollup of 6 pull requests Successful merges: - rust-lang#114959 (fix rust-lang#113702 emit a proper diagnostic message for unstable lints passed from CLI) - rust-lang#115011 (Warn on elided lifetimes in associated constants (`ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT`)) - rust-lang#115077 (Do not emit invalid suggestion in E0191 when spans overlap) - rust-lang#115087 (Add disclaimer on size assertion macro) - rust-lang#115090 (Always use `os-release` rather than `/lib` to detect `NixOS` (bootstrap)) - rust-lang#115101 (triagebot: add dependency licensing pings) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3e9e574 + 266b65a commit 712d962

File tree

24 files changed

+296
-57
lines changed

24 files changed

+296
-57
lines changed

Diff for: compiler/rustc_hir_analysis/src/astconv/errors.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,21 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
597597
}
598598
}
599599
}
600-
if !suggestions.is_empty() {
600+
suggestions.sort_by_key(|&(span, _)| span);
601+
// There are cases where one bound points to a span within another bound's span, like when
602+
// you have code like the following (#115019), so we skip providing a suggestion in those
603+
// cases to avoid having a malformed suggestion.
604+
//
605+
// pub struct Flatten<I> {
606+
// inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::core,
607+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
608+
// | ^^^^^^^^^^^^^^^^^^^^^
609+
// | |
610+
// | associated types `Item`, `IntoIter` must be specified
611+
// associated types `Item`, `IntoIter` must be specified
612+
// }
613+
let overlaps = suggestions.windows(2).any(|pair| pair[0].0.overlaps(pair[1].0));
614+
if !suggestions.is_empty() && !overlaps {
601615
err.multipart_suggestion(
602616
format!("specify the associated type{}", pluralize!(types_count)),
603617
suggestions,

Diff for: compiler/rustc_hir_analysis/src/check/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ fn default_body_is_unstable(
289289
&tcx.sess.parse_sess,
290290
feature,
291291
rustc_feature::GateIssue::Library(issue),
292+
false,
292293
);
293294

294295
err.emit();

Diff for: compiler/rustc_index/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ pub use {idx::Idx, slice::IndexSlice, vec::IndexVec};
2929
pub use rustc_macros::newtype_index;
3030

3131
/// Type size assertion. The first argument is a type and the second argument is its expected size.
32+
///
33+
/// <div class="warning">
34+
///
35+
/// Emitting hard errors from size assertions like this is generally not
36+
/// recommended, especially in libraries, because they can cause build failures if the layout
37+
/// algorithm or dependencies change. Here in rustc we control the toolchain and layout algorithm,
38+
/// so the former is not a problem. For the latter we have a lockfile as rustc is an application and
39+
/// precompiled library.
40+
///
41+
/// Short version: Don't copy this macro into your own code. Use a `#[test]` instead.
42+
///
43+
/// </div>
3244
#[macro_export]
3345
macro_rules! static_assert_size {
3446
($ty:ty, $size:expr) => {

Diff for: compiler/rustc_lint/src/context.rs

+8
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,14 @@ pub trait LintContext: Sized {
966966
Applicability::MachineApplicable
967967
);
968968
}
969+
BuiltinLintDiagnostics::AssociatedConstElidedLifetime { elided, span } => {
970+
db.span_suggestion_verbose(
971+
if elided { span.shrink_to_hi() } else { span },
972+
"use the `'static` lifetime",
973+
if elided { "'static " } else { "'static" },
974+
Applicability::MachineApplicable
975+
);
976+
}
969977
}
970978
// Rewrap `db`, and pass control to the user.
971979
decorate(db)

Diff for: compiler/rustc_lint/src/levels.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_ast as ast;
1212
use rustc_ast_pretty::pprust;
1313
use rustc_data_structures::fx::FxHashMap;
1414
use rustc_errors::{DecorateLint, DiagnosticBuilder, DiagnosticMessage, MultiSpan};
15-
use rustc_feature::Features;
15+
use rustc_feature::{Features, GateIssue};
1616
use rustc_hir as hir;
1717
use rustc_hir::intravisit::{self, Visitor};
1818
use rustc_hir::HirId;
@@ -24,12 +24,14 @@ use rustc_middle::lint::{
2424
};
2525
use rustc_middle::query::Providers;
2626
use rustc_middle::ty::{RegisteredTools, TyCtxt};
27-
use rustc_session::lint::builtin::{RENAMED_AND_REMOVED_LINTS, UNKNOWN_LINTS, UNUSED_ATTRIBUTES};
2827
use rustc_session::lint::{
29-
builtin::{self, FORBIDDEN_LINT_GROUPS, SINGLE_USE_LIFETIMES, UNFULFILLED_LINT_EXPECTATIONS},
28+
builtin::{
29+
self, FORBIDDEN_LINT_GROUPS, RENAMED_AND_REMOVED_LINTS, SINGLE_USE_LIFETIMES,
30+
UNFULFILLED_LINT_EXPECTATIONS, UNKNOWN_LINTS, UNUSED_ATTRIBUTES,
31+
},
3032
Level, Lint, LintExpectationId, LintId,
3133
};
32-
use rustc_session::parse::{add_feature_diagnostics, feature_err};
34+
use rustc_session::parse::feature_err;
3335
use rustc_session::Session;
3436
use rustc_span::symbol::{sym, Symbol};
3537
use rustc_span::{Span, DUMMY_SP};
@@ -566,7 +568,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
566568
continue;
567569
}
568570

569-
if self.check_gated_lint(id, DUMMY_SP) {
571+
if self.check_gated_lint(id, DUMMY_SP, true) {
570572
let src = LintLevelSource::CommandLine(lint_flag_val, orig_level);
571573
self.insert(id, (level, src));
572574
}
@@ -837,7 +839,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
837839
reason,
838840
};
839841
for &id in *ids {
840-
if self.check_gated_lint(id, attr.span) {
842+
if self.check_gated_lint(id, attr.span, false) {
841843
self.insert_spec(id, (level, src));
842844
}
843845
}
@@ -854,7 +856,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
854856
reason,
855857
};
856858
for &id in ids {
857-
if self.check_gated_lint(id, attr.span) {
859+
if self.check_gated_lint(id, attr.span, false) {
858860
self.insert_spec(id, (level, src));
859861
}
860862
}
@@ -955,7 +957,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
955957
reason,
956958
};
957959
for &id in ids {
958-
if self.check_gated_lint(id, attr.span) {
960+
if self.check_gated_lint(id, attr.span, false) {
959961
self.insert_spec(id, (level, src));
960962
}
961963
}
@@ -1000,7 +1002,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
10001002
// FIXME only emit this once for each attribute, instead of repeating it 4 times for
10011003
// pre-expansion lints, post-expansion lints, `shallow_lint_levels_on` and `lint_expectations`.
10021004
#[track_caller]
1003-
fn check_gated_lint(&self, lint_id: LintId, span: Span) -> bool {
1005+
fn check_gated_lint(&self, lint_id: LintId, span: Span, lint_from_cli: bool) -> bool {
10041006
if let Some(feature) = lint_id.lint.feature_gate {
10051007
if !self.features.enabled(feature) {
10061008
let lint = builtin::UNKNOWN_LINTS;
@@ -1015,7 +1017,13 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
10151017
|lint| {
10161018
lint.set_arg("name", lint_id.lint.name_lower());
10171019
lint.note(fluent::lint_note);
1018-
add_feature_diagnostics(lint, &self.sess.parse_sess, feature);
1020+
rustc_session::parse::add_feature_diagnostics_for_issue(
1021+
lint,
1022+
&self.sess.parse_sess,
1023+
feature,
1024+
GateIssue::Language,
1025+
lint_from_cli,
1026+
);
10191027
lint
10201028
},
10211029
);

Diff for: compiler/rustc_lint_defs/src/builtin.rs

+42
Original file line numberDiff line numberDiff line change
@@ -3376,6 +3376,7 @@ declare_lint_pass! {
33763376
DEPRECATED_IN_FUTURE,
33773377
DEPRECATED_WHERE_CLAUSE_LOCATION,
33783378
DUPLICATE_MACRO_ATTRIBUTES,
3379+
ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
33793380
ELIDED_LIFETIMES_IN_PATHS,
33803381
EXPORTED_PRIVATE_DEPENDENCIES,
33813382
FFI_UNWIND_CALLS,
@@ -4527,3 +4528,44 @@ declare_lint! {
45274528
reference: "issue #114095 <https://github.com/rust-lang/rust/issues/114095>",
45284529
};
45294530
}
4531+
4532+
declare_lint! {
4533+
/// The `elided_lifetimes_in_associated_constant` lint detects elided lifetimes
4534+
/// that were erroneously allowed in associated constants.
4535+
///
4536+
/// ### Example
4537+
///
4538+
/// ```rust,compile_fail
4539+
/// #![deny(elided_lifetimes_in_associated_constant)]
4540+
///
4541+
/// struct Foo;
4542+
///
4543+
/// impl Foo {
4544+
/// const STR: &str = "hello, world";
4545+
/// }
4546+
/// ```
4547+
///
4548+
/// {{produces}}
4549+
///
4550+
/// ### Explanation
4551+
///
4552+
/// Previous version of Rust
4553+
///
4554+
/// Implicit static-in-const behavior was decided [against] for associated
4555+
/// constants because of ambiguity. This, however, regressed and the compiler
4556+
/// erroneously treats elided lifetimes in associated constants as lifetime
4557+
/// parameters on the impl.
4558+
///
4559+
/// This is a [future-incompatible] lint to transition this to a
4560+
/// hard error in the future.
4561+
///
4562+
/// [against]: https://github.com/rust-lang/rust/issues/38831
4563+
/// [future-incompatible]: ../index.md#future-incompatible-lints
4564+
pub ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
4565+
Warn,
4566+
"elided lifetimes cannot be used in associated constants in impls",
4567+
@future_incompatible = FutureIncompatibleInfo {
4568+
reason: FutureIncompatibilityReason::FutureReleaseError,
4569+
reference: "issue #115010 <https://github.com/rust-lang/rust/issues/115010>",
4570+
};
4571+
}

Diff for: compiler/rustc_lint_defs/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ pub enum BuiltinLintDiagnostics {
573573
/// The span of the unnecessarily-qualified path to remove.
574574
removal_span: Span,
575575
},
576+
AssociatedConstElidedLifetime {
577+
elided: bool,
578+
span: Span,
579+
},
576580
}
577581

578582
/// Lints that are buffered up early on in the `Session` before the

Diff for: compiler/rustc_resolve/src/late.rs

+55-24
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,10 @@ enum LifetimeRibKind {
311311
/// error on default object bounds (e.g., `Box<dyn Foo>`).
312312
AnonymousReportError,
313313

314+
/// Resolves elided lifetimes to `'static`, but gives a warning that this behavior
315+
/// is a bug and will be reverted soon.
316+
AnonymousWarnToStatic(NodeId),
317+
314318
/// Signal we cannot find which should be the anonymous lifetime.
315319
ElisionFailure,
316320

@@ -1148,6 +1152,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
11481152
}
11491153
LifetimeRibKind::AnonymousCreateParameter { .. }
11501154
| LifetimeRibKind::AnonymousReportError
1155+
| LifetimeRibKind::AnonymousWarnToStatic(_)
11511156
| LifetimeRibKind::Elided(_)
11521157
| LifetimeRibKind::ElisionFailure
11531158
| LifetimeRibKind::ConcreteAnonConst(_)
@@ -1515,6 +1520,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15151520
// lifetime would be illegal.
15161521
LifetimeRibKind::Item
15171522
| LifetimeRibKind::AnonymousReportError
1523+
| LifetimeRibKind::AnonymousWarnToStatic(_)
15181524
| LifetimeRibKind::ElisionFailure => Some(LifetimeUseSet::Many),
15191525
// An anonymous lifetime is legal here, and bound to the right
15201526
// place, go ahead.
@@ -1576,7 +1582,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15761582
| LifetimeRibKind::Elided(_)
15771583
| LifetimeRibKind::Generics { .. }
15781584
| LifetimeRibKind::ElisionFailure
1579-
| LifetimeRibKind::AnonymousReportError => {}
1585+
| LifetimeRibKind::AnonymousReportError
1586+
| LifetimeRibKind::AnonymousWarnToStatic(_) => {}
15801587
}
15811588
}
15821589

@@ -1616,6 +1623,25 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
16161623
self.record_lifetime_res(lifetime.id, res, elision_candidate);
16171624
return;
16181625
}
1626+
LifetimeRibKind::AnonymousWarnToStatic(node_id) => {
1627+
self.record_lifetime_res(lifetime.id, LifetimeRes::Static, elision_candidate);
1628+
let msg = if elided {
1629+
"`&` without an explicit lifetime name cannot be used here"
1630+
} else {
1631+
"`'_` cannot be used here"
1632+
};
1633+
self.r.lint_buffer.buffer_lint_with_diagnostic(
1634+
lint::builtin::ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
1635+
node_id,
1636+
lifetime.ident.span,
1637+
msg,
1638+
lint::BuiltinLintDiagnostics::AssociatedConstElidedLifetime {
1639+
elided,
1640+
span: lifetime.ident.span,
1641+
},
1642+
);
1643+
return;
1644+
}
16191645
LifetimeRibKind::AnonymousReportError => {
16201646
let (msg, note) = if elided {
16211647
(
@@ -1811,7 +1837,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
18111837
//
18121838
// impl Foo for std::cell::Ref<u32> // note lack of '_
18131839
// async fn foo(_: std::cell::Ref<u32>) { ... }
1814-
LifetimeRibKind::AnonymousCreateParameter { report_in_path: true, .. } => {
1840+
LifetimeRibKind::AnonymousCreateParameter { report_in_path: true, .. }
1841+
| LifetimeRibKind::AnonymousWarnToStatic(_) => {
18151842
let sess = self.r.tcx.sess;
18161843
let mut err = rustc_errors::struct_span_err!(
18171844
sess,
@@ -2898,7 +2925,6 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
28982925
match &item.kind {
28992926
AssocItemKind::Const(box ast::ConstItem { generics, ty, expr, .. }) => {
29002927
debug!("resolve_implementation AssocItemKind::Const");
2901-
29022928
self.with_generic_param_rib(
29032929
&generics.params,
29042930
RibKind::AssocItem,
@@ -2908,28 +2934,33 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
29082934
kind: LifetimeBinderKind::ConstItem,
29092935
},
29102936
|this| {
2911-
// If this is a trait impl, ensure the const
2912-
// exists in trait
2913-
this.check_trait_item(
2914-
item.id,
2915-
item.ident,
2916-
&item.kind,
2917-
ValueNS,
2918-
item.span,
2919-
seen_trait_items,
2920-
|i, s, c| ConstNotMemberOfTrait(i, s, c),
2921-
);
2937+
this.with_lifetime_rib(
2938+
LifetimeRibKind::AnonymousWarnToStatic(item.id),
2939+
|this| {
2940+
// If this is a trait impl, ensure the const
2941+
// exists in trait
2942+
this.check_trait_item(
2943+
item.id,
2944+
item.ident,
2945+
&item.kind,
2946+
ValueNS,
2947+
item.span,
2948+
seen_trait_items,
2949+
|i, s, c| ConstNotMemberOfTrait(i, s, c),
2950+
);
29222951

2923-
this.visit_generics(generics);
2924-
this.visit_ty(ty);
2925-
if let Some(expr) = expr {
2926-
// We allow arbitrary const expressions inside of associated consts,
2927-
// even if they are potentially not const evaluatable.
2928-
//
2929-
// Type parameters can already be used and as associated consts are
2930-
// not used as part of the type system, this is far less surprising.
2931-
this.resolve_const_body(expr, None);
2932-
}
2952+
this.visit_generics(generics);
2953+
this.visit_ty(ty);
2954+
if let Some(expr) = expr {
2955+
// We allow arbitrary const expressions inside of associated consts,
2956+
// even if they are potentially not const evaluatable.
2957+
//
2958+
// Type parameters can already be used and as associated consts are
2959+
// not used as part of the type system, this is far less surprising.
2960+
this.resolve_const_body(expr, None);
2961+
}
2962+
},
2963+
);
29332964
},
29342965
);
29352966
}

Diff for: compiler/rustc_session/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ session_cannot_mix_and_match_sanitizers = `-Zsanitizer={$first}` is incompatible
88
session_cgu_not_recorded =
99
CGU-reuse for `{$cgu_user_name}` is (mangled: `{$cgu_name}`) was not recorded
1010
11+
session_cli_feature_diagnostic_help =
12+
add `-Zcrate-attr="feature({$feature})"` to the command-line options to enable
13+
1114
session_crate_name_does_not_match = `--crate-name` and `#[crate_name]` are required to match, but `{$s}` != `{$name}`
1215
1316
session_crate_name_empty = crate name must not be empty

Diff for: compiler/rustc_session/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ pub struct FeatureDiagnosticHelp {
5757
pub feature: Symbol,
5858
}
5959

60+
#[derive(Subdiagnostic)]
61+
#[help(session_cli_feature_diagnostic_help)]
62+
pub struct CliFeatureDiagnosticHelp {
63+
pub feature: Symbol,
64+
}
65+
6066
#[derive(Diagnostic)]
6167
#[diag(session_not_circumvent_feature)]
6268
pub struct NotCircumventFeature;

0 commit comments

Comments
 (0)