Skip to content

Commit c3384ea

Browse files
committed
Auto merge of #97717 - matthiaskrgr:rollup-lalaii2, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #97446 (Make hir().get_generics and generics_of consistent.) - #97656 (Add a suggestion to replace parentheses with angle brackets on associated trait constraint) - #97692 (Add `#T-types/nominated` zulip notification) - #97696 (Do not ICE when failing to normalize during inlining.) - #97702 (Remove useless LocalDefId in ImplTraitContext::Universal.) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents f15370b + a88d94b commit c3384ea

File tree

10 files changed

+156
-107
lines changed

10 files changed

+156
-107
lines changed

compiler/rustc_ast_lowering/src/item.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
266266
let body_id =
267267
this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref());
268268

269-
let itctx = ImplTraitContext::Universal(this.current_hir_id_owner);
269+
let itctx = ImplTraitContext::Universal;
270270
let (generics, decl) = this.lower_generics(generics, id, itctx, |this| {
271271
let ret_id = asyncness.opt_return_id();
272272
this.lower_fn_decl(&decl, Some(id), FnDeclKind::Fn, ret_id)
@@ -385,7 +385,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
385385
// method, it will not be considered an in-band
386386
// lifetime to be added, but rather a reference to a
387387
// parent lifetime.
388-
let itctx = ImplTraitContext::Universal(self.current_hir_id_owner);
388+
let itctx = ImplTraitContext::Universal;
389389
let (generics, (trait_ref, lowered_ty)) =
390390
self.lower_generics(ast_generics, id, itctx, |this| {
391391
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
@@ -655,7 +655,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
655655
kind: match i.kind {
656656
ForeignItemKind::Fn(box Fn { ref sig, ref generics, .. }) => {
657657
let fdec = &sig.decl;
658-
let itctx = ImplTraitContext::Universal(self.current_hir_id_owner);
658+
let itctx = ImplTraitContext::Universal;
659659
let (generics, (fn_dec, fn_args)) =
660660
self.lower_generics(generics, i.id, itctx, |this| {
661661
(
@@ -1237,7 +1237,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12371237
is_async: Option<NodeId>,
12381238
) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) {
12391239
let header = self.lower_fn_header(sig.header);
1240-
let itctx = ImplTraitContext::Universal(self.current_hir_id_owner);
1240+
let itctx = ImplTraitContext::Universal;
12411241
let (generics, decl) = self.lower_generics(generics, id, itctx, |this| {
12421242
this.lower_fn_decl(&sig.decl, Some(id), kind, is_async)
12431243
});

compiler/rustc_ast_lowering/src/lib.rs

+45-21
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4646
use rustc_data_structures::sorted_map::SortedMap;
4747
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4848
use rustc_data_structures::sync::Lrc;
49-
use rustc_errors::struct_span_err;
49+
use rustc_errors::{struct_span_err, Applicability};
5050
use rustc_hir as hir;
5151
use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
5252
use rustc_hir::def_id::{DefId, DefPathHash, LocalDefId, CRATE_DEF_ID};
@@ -253,7 +253,7 @@ enum ImplTraitContext {
253253
/// equivalent to a fresh universal parameter like `fn foo<T: Debug>(x: T)`.
254254
///
255255
/// Newly generated parameters should be inserted into the given `Vec`.
256-
Universal(LocalDefId),
256+
Universal,
257257

258258
/// Treat `impl Trait` as shorthand for a new opaque type.
259259
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
@@ -857,20 +857,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
857857
itctx: ImplTraitContext,
858858
) -> hir::TypeBinding<'hir> {
859859
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
860-
861860
// lower generic arguments of identifier in constraint
862861
let gen_args = if let Some(ref gen_args) = constraint.gen_args {
863862
let gen_args_ctor = match gen_args {
864863
GenericArgs::AngleBracketed(ref data) => {
865864
self.lower_angle_bracketed_parameter_data(data, ParamMode::Explicit, itctx).0
866865
}
867866
GenericArgs::Parenthesized(ref data) => {
868-
let mut err = self.sess.struct_span_err(
869-
gen_args.span(),
870-
"parenthesized generic arguments cannot be used in associated type constraints"
871-
);
872-
// FIXME: try to write a suggestion here
873-
err.emit();
867+
self.assoc_ty_contraint_param_error_emit(data);
874868
self.lower_angle_bracketed_parameter_data(
875869
&data.as_angle_bracketed_args(),
876870
ParamMode::Explicit,
@@ -893,7 +887,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
893887
hir::TypeBindingKind::Equality { term }
894888
}
895889
AssocConstraintKind::Bound { ref bounds } => {
896-
let mut parent_def_id = self.current_hir_id_owner;
897890
// Piggy-back on the `impl Trait` context to figure out the correct behavior.
898891
let (desugar_to_impl_trait, itctx) = match itctx {
899892
// We are in the return position:
@@ -913,10 +906,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
913906
// so desugar to
914907
//
915908
// fn foo(x: dyn Iterator<Item = impl Debug>)
916-
ImplTraitContext::Universal(parent) if self.is_in_dyn_type => {
917-
parent_def_id = parent;
918-
(true, itctx)
919-
}
909+
ImplTraitContext::Universal if self.is_in_dyn_type => (true, itctx),
920910

921911
// In `type Foo = dyn Iterator<Item: Debug>` we desugar to
922912
// `type Foo = dyn Iterator<Item = impl Debug>` but we have to override the
@@ -942,6 +932,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
942932
// Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by
943933
// constructing the HIR for `impl bounds...` and then lowering that.
944934

935+
let parent_def_id = self.current_hir_id_owner;
945936
let impl_trait_node_id = self.resolver.next_node_id();
946937
self.resolver.create_def(
947938
parent_def_id,
@@ -984,6 +975,42 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
984975
}
985976
}
986977

978+
fn assoc_ty_contraint_param_error_emit(&self, data: &ParenthesizedArgs) -> () {
979+
let mut err = self.sess.struct_span_err(
980+
data.span,
981+
"parenthesized generic arguments cannot be used in associated type constraints",
982+
);
983+
// Suggest removing empty parentheses: "Trait()" -> "Trait"
984+
if data.inputs.is_empty() {
985+
let parentheses_span =
986+
data.inputs_span.shrink_to_lo().to(data.inputs_span.shrink_to_hi());
987+
err.multipart_suggestion(
988+
"remove these parentheses",
989+
vec![(parentheses_span, String::new())],
990+
Applicability::MaybeIncorrect,
991+
);
992+
}
993+
// Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
994+
else {
995+
// Start of parameters to the 1st argument
996+
let open_param = data.inputs_span.shrink_to_lo().to(data
997+
.inputs
998+
.first()
999+
.unwrap()
1000+
.span
1001+
.shrink_to_lo());
1002+
// End of last argument to end of parameters
1003+
let close_param =
1004+
data.inputs.last().unwrap().span.shrink_to_hi().to(data.inputs_span.shrink_to_hi());
1005+
err.multipart_suggestion(
1006+
&format!("use angle brackets instead",),
1007+
vec![(open_param, String::from("<")), (close_param, String::from(">"))],
1008+
Applicability::MaybeIncorrect,
1009+
);
1010+
}
1011+
err.emit();
1012+
}
1013+
9871014
fn lower_generic_arg(
9881015
&mut self,
9891016
arg: &ast::GenericArg,
@@ -1184,12 +1211,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11841211
|this| this.lower_param_bounds(bounds, nested_itctx),
11851212
)
11861213
}
1187-
ImplTraitContext::Universal(parent_def_id) => {
1214+
ImplTraitContext::Universal => {
11881215
// Add a definition for the in-band `Param`.
11891216
let def_id = self.resolver.local_def_id(def_node_id);
11901217

1191-
let hir_bounds = self
1192-
.lower_param_bounds(bounds, ImplTraitContext::Universal(parent_def_id));
1218+
let hir_bounds =
1219+
self.lower_param_bounds(bounds, ImplTraitContext::Universal);
11931220
// Set the name to `impl Bound1 + Bound2`.
11941221
let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span);
11951222
let param = hir::GenericParam {
@@ -1399,10 +1426,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13991426
}
14001427
let inputs = self.arena.alloc_from_iter(inputs.iter().map(|param| {
14011428
if fn_node_id.is_some() {
1402-
self.lower_ty_direct(
1403-
&param.ty,
1404-
ImplTraitContext::Universal(self.current_hir_id_owner),
1405-
)
1429+
self.lower_ty_direct(&param.ty, ImplTraitContext::Universal)
14061430
} else {
14071431
self.lower_ty_direct(
14081432
&param.ty,

compiler/rustc_hir/src/hir.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -3004,13 +3004,12 @@ impl ItemKind<'_> {
30043004
Some(match *self {
30053005
ItemKind::Fn(_, ref generics, _)
30063006
| ItemKind::TyAlias(_, ref generics)
3007-
| ItemKind::OpaqueTy(OpaqueTy {
3008-
ref generics, origin: OpaqueTyOrigin::TyAlias, ..
3009-
})
3007+
| ItemKind::OpaqueTy(OpaqueTy { ref generics, .. })
30103008
| ItemKind::Enum(_, ref generics)
30113009
| ItemKind::Struct(_, ref generics)
30123010
| ItemKind::Union(_, ref generics)
30133011
| ItemKind::Trait(_, _, ref generics, _, _)
3012+
| ItemKind::TraitAlias(ref generics, _)
30143013
| ItemKind::Impl(Impl { ref generics, .. }) => generics,
30153014
_ => return None,
30163015
})
@@ -3210,13 +3209,8 @@ impl<'hir> OwnerNode<'hir> {
32103209
}
32113210
}
32123211

3213-
pub fn generics(&self) -> Option<&'hir Generics<'hir>> {
3214-
match self {
3215-
OwnerNode::TraitItem(TraitItem { generics, .. })
3216-
| OwnerNode::ImplItem(ImplItem { generics, .. }) => Some(generics),
3217-
OwnerNode::Item(item) => item.kind.generics(),
3218-
_ => None,
3219-
}
3212+
pub fn generics(self) -> Option<&'hir Generics<'hir>> {
3213+
Node::generics(self.into())
32203214
}
32213215

32223216
pub fn def_id(self) -> LocalDefId {
@@ -3403,9 +3397,12 @@ impl<'hir> Node<'hir> {
34033397
}
34043398
}
34053399

3406-
pub fn generics(&self) -> Option<&'hir Generics<'hir>> {
3400+
pub fn generics(self) -> Option<&'hir Generics<'hir>> {
34073401
match self {
3408-
Node::TraitItem(TraitItem { generics, .. })
3402+
Node::ForeignItem(ForeignItem {
3403+
kind: ForeignItemKind::Fn(_, _, generics), ..
3404+
})
3405+
| Node::TraitItem(TraitItem { generics, .. })
34093406
| Node::ImplItem(ImplItem { generics, .. }) => Some(generics),
34103407
Node::Item(item) => item.kind.generics(),
34113408
_ => None,

compiler/rustc_middle/src/hir/map/mod.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -361,27 +361,7 @@ impl<'hir> Map<'hir> {
361361

362362
pub fn get_generics(self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
363363
let node = self.tcx.hir_owner(id)?;
364-
match node.node {
365-
OwnerNode::ImplItem(impl_item) => Some(&impl_item.generics),
366-
OwnerNode::TraitItem(trait_item) => Some(&trait_item.generics),
367-
OwnerNode::ForeignItem(ForeignItem {
368-
kind: ForeignItemKind::Fn(_, _, generics),
369-
..
370-
})
371-
| OwnerNode::Item(Item {
372-
kind:
373-
ItemKind::Fn(_, generics, _)
374-
| ItemKind::TyAlias(_, generics)
375-
| ItemKind::Enum(_, generics)
376-
| ItemKind::Struct(_, generics)
377-
| ItemKind::Union(_, generics)
378-
| ItemKind::Trait(_, _, generics, ..)
379-
| ItemKind::TraitAlias(generics, _)
380-
| ItemKind::Impl(Impl { generics, .. }),
381-
..
382-
}) => Some(generics),
383-
_ => None,
384-
}
364+
node.node.generics()
385365
}
386366

387367
pub fn item(self, id: ItemId) -> &'hir Item<'hir> {

compiler/rustc_mir_transform/src/inline.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,13 @@ impl<'tcx> Inliner<'tcx> {
158158
return Err("optimization fuel exhausted");
159159
}
160160

161-
let callee_body = callsite.callee.subst_mir_and_normalize_erasing_regions(
161+
let Ok(callee_body) = callsite.callee.try_subst_mir_and_normalize_erasing_regions(
162162
self.tcx,
163163
self.param_env,
164164
callee_body.clone(),
165-
);
165+
) else {
166+
return Err("failed to normalize callee body");
167+
};
166168

167169
let old_blocks = caller_body.basic_blocks().next_index();
168170
self.inline_call(caller_body, &callsite, callee_body);
@@ -253,7 +255,7 @@ impl<'tcx> Inliner<'tcx> {
253255
let func_ty = func.ty(caller_body, self.tcx);
254256
if let ty::FnDef(def_id, substs) = *func_ty.kind() {
255257
// To resolve an instance its substs have to be fully normalized.
256-
let substs = self.tcx.normalize_erasing_regions(self.param_env, substs);
258+
let substs = self.tcx.try_normalize_erasing_regions(self.param_env, substs).ok()?;
257259
let callee =
258260
Instance::resolve(self.tcx, self.param_env, def_id, substs).ok().flatten()?;
259261

@@ -408,14 +410,17 @@ impl<'tcx> Inliner<'tcx> {
408410
if let ty::FnDef(def_id, substs) =
409411
*callsite.callee.subst_mir(self.tcx, &f.literal.ty()).kind()
410412
{
411-
let substs = self.tcx.normalize_erasing_regions(self.param_env, substs);
412-
if let Ok(Some(instance)) =
413-
Instance::resolve(self.tcx, self.param_env, def_id, substs)
413+
if let Ok(substs) =
414+
self.tcx.try_normalize_erasing_regions(self.param_env, substs)
414415
{
415-
if callsite.callee.def_id() == instance.def_id() {
416-
return Err("self-recursion");
417-
} else if self.history.contains(&instance) {
418-
return Err("already inlined");
416+
if let Ok(Some(instance)) =
417+
Instance::resolve(self.tcx, self.param_env, def_id, substs)
418+
{
419+
if callsite.callee.def_id() == instance.def_id() {
420+
return Err("self-recursion");
421+
} else if self.history.contains(&instance) {
422+
return Err("already inlined");
423+
}
419424
}
420425
}
421426
// Don't give intrinsics the extra penalty for calls

compiler/rustc_typeck/src/collect.rs

+13-38
Original file line numberDiff line numberDiff line change
@@ -1588,41 +1588,20 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
15881588
_ => None,
15891589
};
15901590

1591-
let mut opt_self = None;
1592-
let mut allow_defaults = false;
1593-
15941591
let no_generics = hir::Generics::empty();
1595-
let ast_generics = match node {
1596-
Node::TraitItem(item) => &item.generics,
1597-
1598-
Node::ImplItem(item) => &item.generics,
1599-
1592+
let ast_generics = node.generics().unwrap_or(&no_generics);
1593+
let (opt_self, allow_defaults) = match node {
16001594
Node::Item(item) => {
16011595
match item.kind {
1602-
ItemKind::Fn(.., ref generics, _)
1603-
| ItemKind::Impl(hir::Impl { ref generics, .. }) => generics,
1604-
1605-
ItemKind::TyAlias(_, ref generics)
1606-
| ItemKind::Enum(_, ref generics)
1607-
| ItemKind::Struct(_, ref generics)
1608-
| ItemKind::OpaqueTy(hir::OpaqueTy { ref generics, .. })
1609-
| ItemKind::Union(_, ref generics) => {
1610-
allow_defaults = true;
1611-
generics
1612-
}
1613-
1614-
ItemKind::Trait(_, _, ref generics, ..)
1615-
| ItemKind::TraitAlias(ref generics, ..) => {
1596+
ItemKind::Trait(..) | ItemKind::TraitAlias(..) => {
16161597
// Add in the self type parameter.
16171598
//
16181599
// Something of a hack: use the node id for the trait, also as
16191600
// the node id for the Self type parameter.
1620-
let param_id = item.def_id;
1621-
1622-
opt_self = Some(ty::GenericParamDef {
1601+
let opt_self = Some(ty::GenericParamDef {
16231602
index: 0,
16241603
name: kw::SelfUpper,
1625-
def_id: param_id.to_def_id(),
1604+
def_id,
16261605
pure_wrt_drop: false,
16271606
kind: ty::GenericParamDefKind::Type {
16281607
has_default: false,
@@ -1631,21 +1610,17 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
16311610
},
16321611
});
16331612

1634-
allow_defaults = true;
1635-
generics
1613+
(opt_self, true)
16361614
}
1637-
1638-
_ => &no_generics,
1615+
ItemKind::TyAlias(..)
1616+
| ItemKind::Enum(..)
1617+
| ItemKind::Struct(..)
1618+
| ItemKind::OpaqueTy(..)
1619+
| ItemKind::Union(..) => (None, true),
1620+
_ => (None, false),
16391621
}
16401622
}
1641-
1642-
Node::ForeignItem(item) => match item.kind {
1643-
ForeignItemKind::Static(..) => &no_generics,
1644-
ForeignItemKind::Fn(_, _, ref generics) => generics,
1645-
ForeignItemKind::Type => &no_generics,
1646-
},
1647-
1648-
_ => &no_generics,
1623+
_ => (None, false),
16491624
};
16501625

16511626
let has_self = opt_self.is_some();

src/test/ui/generic-associated-types/gat-trait-path-parenthesised-args.rs

+5
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ fn foo<'a>(arg: Box<dyn X<Y('a) = &'a ()>>) {}
1010
//~| ERROR this associated type takes 0 generic arguments but 1 generic argument
1111
//~| ERROR this associated type takes 1 lifetime argument but 0 lifetime arguments
1212

13+
14+
fn bar<'a>(arg: Box<dyn X<Y() = ()>>) {}
15+
//~^ ERROR: parenthesized generic arguments cannot be used
16+
//~| ERROR this associated type takes 1 lifetime argument but 0 lifetime arguments
17+
1318
fn main() {}

0 commit comments

Comments
 (0)