Skip to content

Commit 78d5c04

Browse files
committed
Auto merge of #129841 - matthiaskrgr:rollup-pkavdtl, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #128495 (core: use `compare_bytes` for more slice element types) - #128641 (refactor: standardize duplicate processes in parser) - #129207 (Lint that warns when an elided lifetime ends up being a named lifetime) - #129493 (Create opaque definitions in resolver.) - #129619 (Update stacker to 0.1.17) - #129672 (Make option-like-enum.rs UB-free and portable) - #129780 (add crashtests for several old unfixed ICEs) - #129832 (Remove stray dot in `std::char::from_u32_unchecked` documentation) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 43eaa5c + 2261ffa commit 78d5c04

File tree

77 files changed

+876
-234
lines changed

Some content is hidden

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

77 files changed

+876
-234
lines changed

Cargo.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -4978,15 +4978,15 @@ dependencies = [
49784978

49794979
[[package]]
49804980
name = "stacker"
4981-
version = "0.1.15"
4981+
version = "0.1.17"
49824982
source = "registry+https://github.com/rust-lang/crates.io-index"
4983-
checksum = "c886bd4480155fd3ef527d45e9ac8dd7118a898a46530b7b94c3e21866259fce"
4983+
checksum = "799c883d55abdb5e98af1a7b3f23b9b6de8ecada0ecac058672d7635eb48ca7b"
49844984
dependencies = [
49854985
"cc",
49864986
"cfg-if",
49874987
"libc",
49884988
"psm",
4989-
"winapi",
4989+
"windows-sys 0.59.0",
49904990
]
49914991

49924992
[[package]]

compiler/rustc_ast/src/visit.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ pub enum FnKind<'a> {
6969
Fn(FnCtxt, Ident, &'a FnSig, &'a Visibility, &'a Generics, Option<&'a Block>),
7070

7171
/// E.g., `|x, y| body`.
72-
Closure(&'a ClosureBinder, &'a FnDecl, &'a Expr),
72+
Closure(&'a ClosureBinder, &'a Option<CoroutineKind>, &'a FnDecl, &'a Expr),
7373
}
7474

7575
impl<'a> FnKind<'a> {
7676
pub fn header(&self) -> Option<&'a FnHeader> {
7777
match *self {
7878
FnKind::Fn(_, _, sig, _, _, _) => Some(&sig.header),
79-
FnKind::Closure(_, _, _) => None,
79+
FnKind::Closure(..) => None,
8080
}
8181
}
8282

@@ -90,7 +90,7 @@ impl<'a> FnKind<'a> {
9090
pub fn decl(&self) -> &'a FnDecl {
9191
match self {
9292
FnKind::Fn(_, _, sig, _, _, _) => &sig.decl,
93-
FnKind::Closure(_, decl, _) => decl,
93+
FnKind::Closure(_, _, decl, _) => decl,
9494
}
9595
}
9696

@@ -839,7 +839,7 @@ pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) -> V::Resu
839839
try_visit!(walk_fn_decl(visitor, decl));
840840
visit_opt!(visitor, visit_block, body);
841841
}
842-
FnKind::Closure(binder, decl, body) => {
842+
FnKind::Closure(binder, _coroutine_kind, decl, body) => {
843843
try_visit!(visitor.visit_closure_binder(binder));
844844
try_visit!(walk_fn_decl(visitor, decl));
845845
try_visit!(visitor.visit_expr(body));
@@ -1107,7 +1107,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
11071107
ExprKind::Closure(box Closure {
11081108
binder,
11091109
capture_clause,
1110-
coroutine_kind: _,
1110+
coroutine_kind,
11111111
constness: _,
11121112
movability: _,
11131113
fn_decl,
@@ -1116,7 +1116,11 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
11161116
fn_arg_span: _,
11171117
}) => {
11181118
try_visit!(visitor.visit_capture_by(capture_clause));
1119-
try_visit!(visitor.visit_fn(FnKind::Closure(binder, fn_decl, body), *span, *id))
1119+
try_visit!(visitor.visit_fn(
1120+
FnKind::Closure(binder, coroutine_kind, fn_decl, body),
1121+
*span,
1122+
*id
1123+
))
11201124
}
11211125
ExprKind::Block(block, opt_label) => {
11221126
visit_opt!(visitor, visit_label, opt_label);

compiler/rustc_ast_lowering/src/lib.rs

+7-29
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ use std::collections::hash_map::Entry;
4545
use rustc_ast::node_id::NodeMap;
4646
use rustc_ast::ptr::P;
4747
use rustc_ast::{self as ast, *};
48-
use rustc_ast_pretty::pprust;
4948
use rustc_data_structures::captures::Captures;
5049
use rustc_data_structures::fingerprint::Fingerprint;
5150
use rustc_data_structures::fx::FxIndexSet;
@@ -837,7 +836,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
837836

838837
(hir::ParamName::Fresh, hir::LifetimeParamKind::Elided(kind))
839838
}
840-
LifetimeRes::Static | LifetimeRes::Error => return None,
839+
LifetimeRes::Static { .. } | LifetimeRes::Error => return None,
841840
res => panic!(
842841
"Unexpected lifetime resolution {:?} for {:?} at {:?}",
843842
res, ident, ident.span
@@ -1399,24 +1398,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13991398
self.tcx.dcx().emit_err(errors::NoPreciseCapturesOnApit { span });
14001399
}
14011400

1402-
let span = t.span;
1403-
1404-
// HACK: pprust breaks strings with newlines when the type
1405-
// gets too long. We don't want these to show up in compiler
1406-
// output or built artifacts, so replace them here...
1407-
// Perhaps we should instead format APITs more robustly.
1408-
let ident = Ident::from_str_and_span(
1409-
&pprust::ty_to_string(t).replace('\n', " "),
1410-
span,
1411-
);
1412-
1413-
self.create_def(
1414-
self.current_hir_id_owner.def_id, // FIXME: should this use self.current_def_id_parent?
1415-
*def_node_id,
1416-
ident.name,
1417-
DefKind::TyParam,
1418-
span,
1419-
);
1401+
let def_id = self.local_def_id(*def_node_id);
1402+
let name = self.tcx.item_name(def_id.to_def_id());
1403+
let ident = Ident::new(name, span);
14201404
let (param, bounds, path) = self.lower_universal_param_and_bounds(
14211405
*def_node_id,
14221406
span,
@@ -1618,13 +1602,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16181602
opaque_ty_span: Span,
16191603
lower_item_bounds: impl FnOnce(&mut Self) -> &'hir [hir::GenericBound<'hir>],
16201604
) -> hir::TyKind<'hir> {
1621-
let opaque_ty_def_id = self.create_def(
1622-
self.current_hir_id_owner.def_id, // FIXME: should this use self.current_def_id_parent?
1623-
opaque_ty_node_id,
1624-
kw::Empty,
1625-
DefKind::OpaqueTy,
1626-
opaque_ty_span,
1627-
);
1605+
let opaque_ty_def_id = self.local_def_id(opaque_ty_node_id);
16281606
debug!(?opaque_ty_def_id);
16291607

16301608
// Map from captured (old) lifetime to synthetic (new) lifetime.
@@ -1656,7 +1634,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16561634
}
16571635

16581636
// Opaques do not capture `'static`
1659-
LifetimeRes::Static | LifetimeRes::Error => {
1637+
LifetimeRes::Static { .. } | LifetimeRes::Error => {
16601638
continue;
16611639
}
16621640

@@ -2069,7 +2047,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20692047
hir::LifetimeName::Param(param)
20702048
}
20712049
LifetimeRes::Infer => hir::LifetimeName::Infer,
2072-
LifetimeRes::Static => hir::LifetimeName::Static,
2050+
LifetimeRes::Static { .. } => hir::LifetimeName::Static,
20732051
LifetimeRes::Error => hir::LifetimeName::Error,
20742052
res => panic!(
20752053
"Unexpected lifetime resolution {:?} for {:?} at {:?}",

compiler/rustc_ast_lowering/src/lifetime_collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl<'ast> LifetimeCollectVisitor<'ast> {
2727
self.collected_lifetimes.insert(lifetime);
2828
}
2929
}
30-
LifetimeRes::Static | LifetimeRes::Error => {
30+
LifetimeRes::Static { .. } | LifetimeRes::Error => {
3131
self.collected_lifetimes.insert(lifetime);
3232
}
3333
LifetimeRes::Infer => {}

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14851485

14861486
let disallowed = (!tilde_const_allowed).then(|| match fk {
14871487
FnKind::Fn(_, ident, _, _, _, _) => TildeConstReason::Function { ident: ident.span },
1488-
FnKind::Closure(_, _, _) => TildeConstReason::Closure,
1488+
FnKind::Closure(..) => TildeConstReason::Closure,
14891489
});
14901490
self.with_tilde_const(disallowed, |this| visit::walk_fn(this, fk));
14911491
}

compiler/rustc_data_structures/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ rustc_index = { path = "../rustc_index", package = "rustc_index" }
2222
rustc_macros = { path = "../rustc_macros" }
2323
rustc_serialize = { path = "../rustc_serialize" }
2424
smallvec = { version = "1.8.1", features = ["const_generics", "union", "may_dangle"] }
25-
stacker = "0.1.15"
25+
stacker = "0.1.17"
2626
tempfile = "3.2"
2727
thin-vec = "0.2.12"
2828
tracing = "0.1"

compiler/rustc_hir/src/def.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,13 @@ pub enum LifetimeRes {
863863
/// This variant is used for anonymous lifetimes that we did not resolve during
864864
/// late resolution. Those lifetimes will be inferred by typechecking.
865865
Infer,
866-
/// Explicit `'static` lifetime.
867-
Static,
866+
/// `'static` lifetime.
867+
Static {
868+
/// We do not want to emit `elided_named_lifetimes`
869+
/// when we are inside of a const item or a static,
870+
/// because it would get too annoying.
871+
suppress_elision_warning: bool,
872+
},
868873
/// Resolution failure.
869874
Error,
870875
/// HACK: This is used to recover the NodeId of an elided lifetime.

compiler/rustc_lint/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ lint_duplicate_macro_attribute =
252252
253253
lint_duplicate_matcher_binding = duplicate matcher binding
254254
255+
lint_elided_named_lifetime = elided lifetime has a name
256+
.label_elided = this elided lifetime gets resolved as `{$name}`
257+
.label_named = lifetime `{$name}` declared here
258+
255259
lint_enum_intrinsics_mem_discriminant =
256260
the return value of `mem::discriminant` is unspecified when called with a non-enum type
257261
.note = the argument to `discriminant` should be a reference to an enum, but it was passed a reference to a `{$ty_param}`, which is not an enum

compiler/rustc_lint/src/context/diagnostics.rs

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_errors::{
1010
use rustc_middle::middle::stability;
1111
use rustc_session::lint::BuiltinLintDiag;
1212
use rustc_session::Session;
13+
use rustc_span::symbol::kw;
1314
use rustc_span::BytePos;
1415
use tracing::debug;
1516

@@ -441,5 +442,17 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
441442
BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by } => {
442443
lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag)
443444
}
445+
BuiltinLintDiag::ElidedIsStatic { elided } => {
446+
lints::ElidedNamedLifetime { elided, name: kw::StaticLifetime, named_declaration: None }
447+
.decorate_lint(diag)
448+
}
449+
BuiltinLintDiag::ElidedIsParam { elided, param: (param_name, param_span) } => {
450+
lints::ElidedNamedLifetime {
451+
elided,
452+
name: param_name,
453+
named_declaration: Some(param_span),
454+
}
455+
.decorate_lint(diag)
456+
}
444457
}
445458
}

compiler/rustc_lint/src/lints.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2611,6 +2611,16 @@ pub(crate) struct ElidedLifetimesInPaths {
26112611
pub subdiag: ElidedLifetimeInPathSubdiag,
26122612
}
26132613

2614+
#[derive(LintDiagnostic)]
2615+
#[diag(lint_elided_named_lifetime)]
2616+
pub(crate) struct ElidedNamedLifetime {
2617+
#[label(lint_label_elided)]
2618+
pub elided: Span,
2619+
pub name: Symbol,
2620+
#[label(lint_label_named)]
2621+
pub named_declaration: Option<Span>,
2622+
}
2623+
26142624
#[derive(LintDiagnostic)]
26152625
#[diag(lint_invalid_crate_type_value)]
26162626
pub(crate) struct UnknownCrateTypes {

compiler/rustc_lint_defs/src/builtin.rs

+33
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ declare_lint_pass! {
4242
DUPLICATE_MACRO_ATTRIBUTES,
4343
ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
4444
ELIDED_LIFETIMES_IN_PATHS,
45+
ELIDED_NAMED_LIFETIMES,
4546
EXPLICIT_BUILTIN_CFGS_IN_FLAGS,
4647
EXPORTED_PRIVATE_DEPENDENCIES,
4748
FFI_UNWIND_CALLS,
@@ -1862,6 +1863,38 @@ declare_lint! {
18621863
"hidden lifetime parameters in types are deprecated"
18631864
}
18641865

1866+
declare_lint! {
1867+
/// The `elided_named_lifetimes` lint detects when an elided
1868+
/// lifetime ends up being a named lifetime, such as `'static`
1869+
/// or some lifetime parameter `'a`.
1870+
///
1871+
/// ### Example
1872+
///
1873+
/// ```rust,compile_fail
1874+
/// #![deny(elided_named_lifetimes)]
1875+
/// struct Foo;
1876+
/// impl Foo {
1877+
/// pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 {
1878+
/// unsafe { &mut *(x as *mut _) }
1879+
/// }
1880+
/// }
1881+
/// ```
1882+
///
1883+
/// {{produces}}
1884+
///
1885+
/// ### Explanation
1886+
///
1887+
/// Lifetime elision is quite useful, because it frees you from having
1888+
/// to give each lifetime its own name, but sometimes it can produce
1889+
/// somewhat surprising resolutions. In safe code, it is mostly okay,
1890+
/// because the borrow checker prevents any unsoundness, so the worst
1891+
/// case scenario is you get a confusing error message in some other place.
1892+
/// But with `unsafe` code, such unexpected resolutions may lead to unsound code.
1893+
pub ELIDED_NAMED_LIFETIMES,
1894+
Warn,
1895+
"detects when an elided lifetime gets resolved to be `'static` or some named parameter"
1896+
}
1897+
18651898
declare_lint! {
18661899
/// The `bare_trait_objects` lint suggests using `dyn Trait` for trait
18671900
/// objects.

compiler/rustc_lint_defs/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,13 @@ pub enum BuiltinLintDiag {
589589
},
590590
MacroExpandedMacroExportsAccessedByAbsolutePaths(Span),
591591
ElidedLifetimesInPaths(usize, Span, bool, Span),
592+
ElidedIsStatic {
593+
elided: Span,
594+
},
595+
ElidedIsParam {
596+
elided: Span,
597+
param: (Symbol, Span),
598+
},
592599
UnknownCrateTypes {
593600
span: Span,
594601
candidate: Option<Symbol>,

0 commit comments

Comments
 (0)