Skip to content

Commit 190a5f0

Browse files
committed
Remove extraneous Res checks in ast_lowering
1 parent 5e85856 commit 190a5f0

13 files changed

+70
-264
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use rustc_data_structures::sorted_map::SortedMap;
4949
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5050
use rustc_data_structures::sync::Lrc;
5151
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
52-
use rustc_hir::def::{CtorKind, DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
52+
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5353
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5454
use rustc_hir::{
5555
self as hir, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem, ParamName, TraitCandidate,
@@ -2060,55 +2060,49 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20602060
ty_id: NodeId,
20612061
span: Span,
20622062
) -> &'hir hir::ConstArg<'hir> {
2063-
let ct_kind = match res {
2064-
// FIXME(min_generic_const_args): only allow one-segment const paths for now
2065-
Res::Def(
2066-
DefKind::ConstParam | DefKind::Const | DefKind::Ctor(_, CtorKind::Const),
2067-
_,
2068-
) if path.is_potential_trivial_const_arg() => {
2069-
let qpath = self.lower_qpath(
2070-
ty_id,
2071-
&None,
2072-
path,
2073-
ParamMode::Optional,
2074-
AllowReturnTypeNotation::No,
2075-
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
2076-
None,
2077-
);
2078-
hir::ConstArgKind::Path(qpath)
2079-
}
2080-
_ => {
2081-
// Construct an AnonConst where the expr is the "ty"'s path.
2063+
// FIXME(min_generic_const_args): we only allow one-segment const paths for now
2064+
let ct_kind = if path.is_potential_trivial_const_arg() {
2065+
let qpath = self.lower_qpath(
2066+
ty_id,
2067+
&None,
2068+
path,
2069+
ParamMode::Optional,
2070+
AllowReturnTypeNotation::No,
2071+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
2072+
None,
2073+
);
2074+
hir::ConstArgKind::Path(qpath)
2075+
} else {
2076+
// Construct an AnonConst where the expr is the "ty"'s path.
20822077

2083-
let parent_def_id = self.current_def_id_parent;
2084-
let node_id = self.next_node_id();
2085-
let span = self.lower_span(span);
2078+
let parent_def_id = self.current_def_id_parent;
2079+
let node_id = self.next_node_id();
2080+
let span = self.lower_span(span);
20862081

2087-
// Add a definition for the in-band const def.
2088-
let def_id =
2089-
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, span);
2090-
let hir_id = self.lower_node_id(node_id);
2082+
// Add a definition for the in-band const def.
2083+
let def_id =
2084+
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, span);
2085+
let hir_id = self.lower_node_id(node_id);
20912086

2092-
let path_expr = Expr {
2093-
id: ty_id,
2094-
kind: ExprKind::Path(None, path.clone()),
2095-
span,
2096-
attrs: AttrVec::new(),
2097-
tokens: None,
2098-
};
2087+
let path_expr = Expr {
2088+
id: ty_id,
2089+
kind: ExprKind::Path(None, path.clone()),
2090+
span,
2091+
attrs: AttrVec::new(),
2092+
tokens: None,
2093+
};
20992094

2100-
let ct = self.with_new_scopes(span, |this| {
2101-
self.arena.alloc(hir::AnonConst {
2102-
def_id,
2103-
hir_id,
2104-
body: this.with_def_id_parent(def_id, |this| {
2105-
this.lower_const_body(path_expr.span, Some(&path_expr))
2106-
}),
2107-
span,
2108-
})
2109-
});
2110-
hir::ConstArgKind::Anon(ct)
2111-
}
2095+
let ct = self.with_new_scopes(span, |this| {
2096+
self.arena.alloc(hir::AnonConst {
2097+
def_id,
2098+
hir_id,
2099+
body: this.with_def_id_parent(def_id, |this| {
2100+
this.lower_const_body(path_expr.span, Some(&path_expr))
2101+
}),
2102+
span,
2103+
})
2104+
});
2105+
hir::ConstArgKind::Anon(ct)
21122106
};
21132107

21142108
self.arena.alloc(hir::ConstArg {
@@ -2137,19 +2131,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21372131
} else {
21382132
&anon.value
21392133
};
2140-
let maybe_res =
2141-
self.resolver.get_partial_res(expr.id).and_then(|partial_res| partial_res.full_res());
2142-
debug!("res={:?}", maybe_res);
2143-
// FIXME(min_generic_const_args): for now we only lower params to ConstArgKind::Path
2144-
if let Some(res) = maybe_res
2145-
&& let ExprKind::Path(qself, path) = &expr.kind
2146-
// FIXME(min_generic_const_args): only allow one-segment const paths for now
2147-
&& let Res::Def(
2148-
DefKind::ConstParam
2149-
| DefKind::Const
2150-
| DefKind::Ctor(_, CtorKind::Const),
2151-
_,
2152-
) = res
2134+
if let ExprKind::Path(qself, path) = &expr.kind
2135+
// FIXME(min_generic_const_args): we only allow one-segment const paths for now
21532136
&& path.is_potential_trivial_const_arg()
21542137
{
21552138
let qpath = self.lower_qpath(

tests/ui/const-generics/const-param-type-depends-on-const-param.min.stderr

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,6 @@ LL | pub struct SelfDependent<const N: [u8; N]>;
1414
|
1515
= note: const parameters may not be used in the type of const parameters
1616

17-
error: `[u8; N]` is forbidden as the type of a const generic parameter
18-
--> $DIR/const-param-type-depends-on-const-param.rs:11:47
19-
|
20-
LL | pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
21-
| ^^^^^^^
22-
|
23-
= note: the only supported types are integers, `bool`, and `char`
24-
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
25-
|
26-
LL + #![feature(adt_const_params)]
27-
|
28-
29-
error: `[u8; N]` is forbidden as the type of a const generic parameter
30-
--> $DIR/const-param-type-depends-on-const-param.rs:15:35
31-
|
32-
LL | pub struct SelfDependent<const N: [u8; N]>;
33-
| ^^^^^^^
34-
|
35-
= note: the only supported types are integers, `bool`, and `char`
36-
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
37-
|
38-
LL + #![feature(adt_const_params)]
39-
|
40-
41-
error: aborting due to 4 previous errors
17+
error: aborting due to 2 previous errors
4218

4319
For more information about this error, try `rustc --explain E0770`.

tests/ui/const-generics/fn-const-param-infer.adt_const_params.stderr

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,7 @@ LL | let _ = Checked::<{ generic_arg::<u32> }>;
1313
= note: expected fn pointer `fn(usize) -> _`
1414
found fn item `fn(u32) -> _ {generic_arg::<u32>}`
1515

16-
error[E0282]: type annotations needed
17-
--> $DIR/fn-const-param-infer.rs:35:23
18-
|
19-
LL | let _ = Checked::<generic>;
20-
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
21-
|
22-
help: consider specifying the generic argument
23-
|
24-
LL | let _ = Checked::<generic::<T>>;
25-
| +++++
26-
27-
error: aborting due to 3 previous errors
16+
error: aborting due to 2 previous errors
2817

29-
Some errors have detailed explanations: E0282, E0308, E0741.
30-
For more information about an error, try `rustc --explain E0282`.
18+
Some errors have detailed explanations: E0308, E0741.
19+
For more information about an error, try `rustc --explain E0308`.

tests/ui/const-generics/fn-const-param-infer.full.stderr

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,7 @@ LL | let _ = Checked::<{ generic_arg::<u32> }>;
1313
= note: expected fn pointer `fn(usize) -> _`
1414
found fn item `fn(u32) -> _ {generic_arg::<u32>}`
1515

16-
error[E0282]: type annotations needed
17-
--> $DIR/fn-const-param-infer.rs:35:23
18-
|
19-
LL | let _ = Checked::<generic>;
20-
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
21-
|
22-
help: consider specifying the generic argument
23-
|
24-
LL | let _ = Checked::<generic::<T>>;
25-
| +++++
26-
27-
error: aborting due to 3 previous errors
16+
error: aborting due to 2 previous errors
2817

29-
Some errors have detailed explanations: E0282, E0308, E0741.
30-
For more information about an error, try `rustc --explain E0282`.
18+
Some errors have detailed explanations: E0308, E0741.
19+
For more information about an error, try `rustc --explain E0308`.

tests/ui/const-generics/fn-const-param-infer.min.stderr

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ LL | let _ = Checked::<{ generic_arg::<u32> }>;
1515
= note: expected fn pointer `fn(usize) -> _`
1616
found fn item `fn(u32) -> _ {generic_arg::<u32>}`
1717

18-
error[E0282]: type annotations needed
19-
--> $DIR/fn-const-param-infer.rs:35:23
20-
|
21-
LL | let _ = Checked::<generic>;
22-
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the function `generic`
23-
|
24-
help: consider specifying the generic argument
25-
|
26-
LL | let _ = Checked::<generic::<T>>;
27-
| +++++
28-
29-
error: aborting due to 3 previous errors
18+
error: aborting due to 2 previous errors
3019

31-
Some errors have detailed explanations: E0282, E0308.
32-
For more information about an error, try `rustc --explain E0282`.
20+
For more information about this error, try `rustc --explain E0308`.

tests/ui/const-generics/generic_const_exprs/error_in_ty.stderr

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,6 @@ LL | pub struct A<const z: [usize; x]> {}
66
| |
77
| similarly named const parameter `z` defined here
88

9-
error: `[usize; x]` is forbidden as the type of a const generic parameter
10-
--> $DIR/error_in_ty.rs:6:23
11-
|
12-
LL | pub struct A<const z: [usize; x]> {}
13-
| ^^^^^^^^^^
14-
|
15-
= note: the only supported types are integers, `bool`, and `char`
16-
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
17-
|
18-
LL + #![feature(adt_const_params)]
19-
|
20-
21-
error[E0308]: mismatched types
22-
--> $DIR/error_in_ty.rs:10:8
23-
|
24-
LL | impl A<2> {
25-
| ^ expected `[usize; x]`, found integer
26-
27-
error[E0308]: mismatched types
28-
--> $DIR/error_in_ty.rs:16:8
29-
|
30-
LL | impl A<2> {
31-
| ^ expected `[usize; x]`, found integer
32-
339
error[E0592]: duplicate definitions with name `B`
3410
--> $DIR/error_in_ty.rs:12:5
3511
|
@@ -39,7 +15,7 @@ LL | pub const fn B() {}
3915
LL | pub const fn B() {}
4016
| ---------------- other definition for `B`
4117

42-
error: aborting due to 5 previous errors
18+
error: aborting due to 2 previous errors
4319

44-
Some errors have detailed explanations: E0308, E0425, E0592.
45-
For more information about an error, try `rustc --explain E0308`.
20+
Some errors have detailed explanations: E0425, E0592.
21+
For more information about an error, try `rustc --explain E0425`.

tests/ui/const-generics/issues/issue-62878.min.stderr

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@ LL | fn foo<const N: usize, const A: [u8; N]>() {}
66
|
77
= note: const parameters may not be used in the type of const parameters
88

9-
error: `[u8; N]` is forbidden as the type of a const generic parameter
10-
--> $DIR/issue-62878.rs:5:33
11-
|
12-
LL | fn foo<const N: usize, const A: [u8; N]>() {}
13-
| ^^^^^^^
14-
|
15-
= note: the only supported types are integers, `bool`, and `char`
16-
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
17-
|
18-
LL + #![feature(adt_const_params)]
19-
|
20-
219
error[E0747]: type provided when a constant was expected
2210
--> $DIR/issue-62878.rs:10:11
2311
|
@@ -30,7 +18,7 @@ help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
3018
LL + #![feature(generic_arg_infer)]
3119
|
3220

33-
error: aborting due to 3 previous errors
21+
error: aborting due to 2 previous errors
3422

3523
Some errors have detailed explanations: E0747, E0770.
3624
For more information about an error, try `rustc --explain E0747`.

tests/ui/const-generics/issues/issue-71169.min.stderr

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@ LL | fn foo<const LEN: usize, const DATA: [u8; LEN]>() {}
66
|
77
= note: const parameters may not be used in the type of const parameters
88

9-
error: `[u8; LEN]` is forbidden as the type of a const generic parameter
10-
--> $DIR/issue-71169.rs:5:38
11-
|
12-
LL | fn foo<const LEN: usize, const DATA: [u8; LEN]>() {}
13-
| ^^^^^^^^^
14-
|
15-
= note: the only supported types are integers, `bool`, and `char`
16-
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
17-
|
18-
LL + #![feature(adt_const_params)]
19-
|
20-
21-
error: aborting due to 2 previous errors
9+
error: aborting due to 1 previous error
2210

2311
For more information about this error, try `rustc --explain E0770`.

tests/ui/const-generics/not_wf_param_in_rpitit.stderr

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,6 @@ LL | trait Trait<const N: dyn Trait = bar> {
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1919
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
2020

21-
error[E0038]: the trait `Trait` cannot be made into an object
22-
--> $DIR/not_wf_param_in_rpitit.rs:3:22
23-
|
24-
LL | trait Trait<const N: dyn Trait = bar> {
25-
| ^^^^^^^^^ `Trait` cannot be made into an object
26-
|
27-
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
28-
--> $DIR/not_wf_param_in_rpitit.rs:9:14
29-
|
30-
LL | trait Trait<const N: dyn Trait = bar> {
31-
| ----- this trait cannot be made into an object...
32-
...
33-
LL | async fn a() {}
34-
| ^ ...because associated function `a` has no `self` parameter
35-
help: consider turning `a` into a method by giving it a `&self` argument
36-
|
37-
LL | async fn a(&self) {}
38-
| +++++
39-
help: alternatively, consider constraining `a` so it does not apply to trait objects
40-
|
41-
LL | async fn a() where Self: Sized {}
42-
| +++++++++++++++++
43-
4421
error[E0038]: the trait `Trait` cannot be made into an object
4522
--> $DIR/not_wf_param_in_rpitit.rs:3:13
4623
|
@@ -88,7 +65,7 @@ help: alternatively, consider constraining `a` so it does not apply to trait obj
8865
LL | async fn a() where Self: Sized {}
8966
| +++++++++++++++++
9067

91-
error: aborting due to 5 previous errors
68+
error: aborting due to 4 previous errors
9269

9370
Some errors have detailed explanations: E0038, E0391, E0425.
9471
For more information about an error, try `rustc --explain E0038`.

tests/ui/generics/generic-function-item-where-type.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ error[E0747]: constant provided when a type was expected
33
|
44
LL | foo::<main>()
55
| ^^^^
6+
|
7+
= help: `main` is a function item, not a type
8+
= help: function item types cannot be named directly
69

710
error: aborting due to 1 previous error
811

tests/ui/privacy/privacy-ns1.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ error[E0747]: constant provided when a type was expected
5757
|
5858
LL | let _x: Box<Bar>;
5959
| ^^^
60+
|
61+
= help: `Bar` is a function item, not a type
62+
= help: function item types cannot be named directly
6063

6164
error: aborting due to 4 previous errors
6265

tests/ui/privacy/privacy-ns2.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,18 @@ error[E0747]: constant provided when a type was expected
8383
|
8484
LL | let _x : Box<Bar>;
8585
| ^^^
86+
|
87+
= help: `Bar` is a function item, not a type
88+
= help: function item types cannot be named directly
8689

8790
error[E0747]: constant provided when a type was expected
8891
--> $DIR/privacy-ns2.rs:48:17
8992
|
9093
LL | let _x: Box<Bar>;
9194
| ^^^
95+
|
96+
= help: `Bar` is a function item, not a type
97+
= help: function item types cannot be named directly
9298

9399
error: aborting due to 8 previous errors
94100

0 commit comments

Comments
 (0)