Skip to content

Commit 57276ce

Browse files
committed
Remove extraneous Res checks in ast_lowering
1 parent ef9b72b commit 57276ce

13 files changed

+70
-264
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+42-59
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use rustc_data_structures::sorted_map::SortedMap;
5252
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5353
use rustc_data_structures::sync::Lrc;
5454
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
55-
use rustc_hir::def::{CtorKind, DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
55+
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5656
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId, LocalDefIdMap};
5757
use rustc_hir::{
5858
self as hir, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem, MissingLifetimeKind,
@@ -2298,55 +2298,49 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22982298
ty_id: NodeId,
22992299
span: Span,
23002300
) -> &'hir hir::ConstArg<'hir> {
2301-
let ct_kind = match res {
2302-
// FIXME(min_generic_const_args): only allow one-segment const paths for now
2303-
Res::Def(
2304-
DefKind::ConstParam | DefKind::Const | DefKind::Ctor(_, CtorKind::Const),
2305-
_,
2306-
) if path.is_potential_trivial_const_arg() => {
2307-
let qpath = self.lower_qpath(
2308-
ty_id,
2309-
&None,
2310-
path,
2311-
ParamMode::Optional,
2312-
AllowReturnTypeNotation::No,
2313-
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
2314-
None,
2315-
);
2316-
hir::ConstArgKind::Path(qpath)
2317-
}
2318-
_ => {
2319-
// Construct an AnonConst where the expr is the "ty"'s path.
2301+
// FIXME(min_generic_const_args): we only allow one-segment const paths for now
2302+
let ct_kind = if path.is_potential_trivial_const_arg() {
2303+
let qpath = self.lower_qpath(
2304+
ty_id,
2305+
&None,
2306+
path,
2307+
ParamMode::Optional,
2308+
AllowReturnTypeNotation::No,
2309+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
2310+
None,
2311+
);
2312+
hir::ConstArgKind::Path(qpath)
2313+
} else {
2314+
// Construct an AnonConst where the expr is the "ty"'s path.
23202315

2321-
let parent_def_id = self.current_def_id_parent;
2322-
let node_id = self.next_node_id();
2323-
let span = self.lower_span(span);
2316+
let parent_def_id = self.current_def_id_parent;
2317+
let node_id = self.next_node_id();
2318+
let span = self.lower_span(span);
23242319

2325-
// Add a definition for the in-band const def.
2326-
let def_id =
2327-
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, span);
2328-
let hir_id = self.lower_node_id(node_id);
2320+
// Add a definition for the in-band const def.
2321+
let def_id =
2322+
self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, span);
2323+
let hir_id = self.lower_node_id(node_id);
23292324

2330-
let path_expr = Expr {
2331-
id: ty_id,
2332-
kind: ExprKind::Path(None, path.clone()),
2333-
span,
2334-
attrs: AttrVec::new(),
2335-
tokens: None,
2336-
};
2325+
let path_expr = Expr {
2326+
id: ty_id,
2327+
kind: ExprKind::Path(None, path.clone()),
2328+
span,
2329+
attrs: AttrVec::new(),
2330+
tokens: None,
2331+
};
23372332

2338-
let ct = self.with_new_scopes(span, |this| {
2339-
self.arena.alloc(hir::AnonConst {
2340-
def_id,
2341-
hir_id,
2342-
body: this.with_def_id_parent(def_id, |this| {
2343-
this.lower_const_body(path_expr.span, Some(&path_expr))
2344-
}),
2345-
span,
2346-
})
2347-
});
2348-
hir::ConstArgKind::Anon(ct)
2349-
}
2333+
let ct = self.with_new_scopes(span, |this| {
2334+
self.arena.alloc(hir::AnonConst {
2335+
def_id,
2336+
hir_id,
2337+
body: this.with_def_id_parent(def_id, |this| {
2338+
this.lower_const_body(path_expr.span, Some(&path_expr))
2339+
}),
2340+
span,
2341+
})
2342+
});
2343+
hir::ConstArgKind::Anon(ct)
23502344
};
23512345

23522346
self.arena.alloc(hir::ConstArg {
@@ -2375,19 +2369,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23752369
} else {
23762370
&anon.value
23772371
};
2378-
let maybe_res =
2379-
self.resolver.get_partial_res(expr.id).and_then(|partial_res| partial_res.full_res());
2380-
debug!("res={:?}", maybe_res);
2381-
// FIXME(min_generic_const_args): for now we only lower params to ConstArgKind::Path
2382-
if let Some(res) = maybe_res
2383-
&& let ExprKind::Path(qself, path) = &expr.kind
2384-
// FIXME(min_generic_const_args): only allow one-segment const paths for now
2385-
&& let Res::Def(
2386-
DefKind::ConstParam
2387-
| DefKind::Const
2388-
| DefKind::Ctor(_, CtorKind::Const),
2389-
_,
2390-
) = res
2372+
if let ExprKind::Path(qself, path) = &expr.kind
2373+
// FIXME(min_generic_const_args): we only allow one-segment const paths for now
23912374
&& path.is_potential_trivial_const_arg()
23922375
{
23932376
let qpath = self.lower_qpath(

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

+1-25
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

+3-14
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

+3-14
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

+2-14
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

+3-27
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

+1-13
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

+1-13
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

+1-24
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

+3
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

+3
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

+6
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)