Skip to content

Commit 872fae6

Browse files
committed
Create a single long-lived InferCtxt for ItemCtxt
1 parent 6e5912f commit 872fae6

13 files changed

+64
-312
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use rustc_target::spec::abi;
3838
use rustc_trait_selection::infer::InferCtxtExt;
3939
use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName;
4040
use rustc_trait_selection::traits::ObligationCtxt;
41-
use std::cell::Cell;
4241
use std::iter;
4342
use std::ops::Bound;
4443

@@ -120,7 +119,7 @@ pub fn provide(providers: &mut Providers) {
120119
pub struct ItemCtxt<'tcx> {
121120
tcx: TyCtxt<'tcx>,
122121
item_def_id: LocalDefId,
123-
tainted_by_errors: Cell<Option<ErrorGuaranteed>>,
122+
infcx: InferCtxt<'tcx>,
124123
}
125124

126125
///////////////////////////////////////////////////////////////////////////
@@ -342,7 +341,7 @@ fn bad_placeholder<'tcx>(
342341

343342
impl<'tcx> ItemCtxt<'tcx> {
344343
pub fn new(tcx: TyCtxt<'tcx>, item_def_id: LocalDefId) -> ItemCtxt<'tcx> {
345-
ItemCtxt { tcx, item_def_id, tainted_by_errors: Cell::new(None) }
344+
ItemCtxt { tcx, item_def_id, infcx: tcx.infer_ctxt().ignoring_regions().build() }
346345
}
347346

348347
pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
@@ -358,7 +357,7 @@ impl<'tcx> ItemCtxt<'tcx> {
358357
}
359358

360359
fn check_tainted_by_errors(&self) -> Result<(), ErrorGuaranteed> {
361-
match self.tainted_by_errors.get() {
360+
match self.infcx.tainted_by_errors() {
362361
Some(err) => Err(err),
363362
None => Ok(()),
364363
}
@@ -503,12 +502,8 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
503502
// There's no place to record types from signatures?
504503
}
505504

506-
fn infcx(&self) -> Option<&InferCtxt<'tcx>> {
507-
None
508-
}
509-
510-
fn set_tainted_by_errors(&self, err: ErrorGuaranteed) {
511-
self.tainted_by_errors.set(Some(err));
505+
fn infcx(&self) -> &InferCtxt<'tcx> {
506+
&self.infcx
512507
}
513508
}
514509

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
3636
use rustc_hir::def_id::{DefId, LocalDefId};
3737
use rustc_hir::intravisit::{walk_generics, Visitor as _};
3838
use rustc_hir::{GenericArg, GenericArgs, HirId};
39-
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
39+
use rustc_infer::infer::InferCtxt;
4040
use rustc_infer::traits::ObligationCause;
4141
use rustc_middle::middle::stability::AllowUnstable;
4242
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
@@ -162,14 +162,16 @@ pub trait HirTyLowerer<'tcx> {
162162
/// Record the lowered type of a HIR node in this context.
163163
fn record_ty(&self, hir_id: HirId, ty: Ty<'tcx>, span: Span);
164164

165-
/// The inference context of the lowering context if applicable.
166-
fn infcx(&self) -> Option<&InferCtxt<'tcx>>;
165+
/// The inference context of the lowering context.
166+
fn infcx(&self) -> &InferCtxt<'tcx>;
167167

168168
/// Taint the context with errors.
169169
///
170170
/// Invoke this when you encounter an error from some prior pass like name resolution.
171171
/// This is used to help suppress derived errors typeck might otherwise report.
172-
fn set_tainted_by_errors(&self, e: ErrorGuaranteed);
172+
fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
173+
self.infcx().set_tainted_by_errors(e);
174+
}
173175

174176
/// Convenience method for coercing the lowering context into a trait object type.
175177
///
@@ -1226,17 +1228,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12261228
// Select applicable inherent associated type candidates modulo regions.
12271229
//
12281230

1229-
// In contexts that have no inference context, just make a new one.
1230-
// We do need a local variable to store it, though.
1231-
let infcx_;
1232-
let infcx = match self.infcx() {
1233-
Some(infcx) => infcx,
1234-
None => {
1235-
assert!(!self_ty.has_infer());
1236-
infcx_ = tcx.infer_ctxt().ignoring_regions().build();
1237-
&infcx_
1238-
}
1239-
};
1231+
let infcx = self.infcx();
1232+
assert!(self.allow_infer() || !self_ty.has_infer());
12401233

12411234
// FIXME(inherent_associated_types): Acquiring the ParamEnv this early leads to cycle errors
12421235
// when inside of an ADT (#108491) or where clause.
@@ -1399,16 +1392,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
13991392
) -> Vec<String> {
14001393
let tcx = self.tcx();
14011394

1402-
// In contexts that have no inference context, just make a new one.
1403-
// We do need a local variable to store it, though.
1404-
let infcx_;
1405-
let infcx = if let Some(infcx) = self.infcx() {
1406-
infcx
1407-
} else {
1408-
assert!(!qself_ty.has_infer());
1409-
infcx_ = tcx.infer_ctxt().build();
1410-
&infcx_
1411-
};
1395+
let infcx = self.infcx();
1396+
assert!(self.allow_infer() || !qself_ty.has_infer());
14121397

14131398
tcx.all_traits()
14141399
.filter(|trait_def_id| {

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
343343
self.write_ty(hir_id, ty)
344344
}
345345

346-
fn infcx(&self) -> Option<&infer::InferCtxt<'tcx>> {
347-
Some(&self.infcx)
346+
fn infcx(&self) -> &infer::InferCtxt<'tcx> {
347+
&self.infcx
348348
}
349349

350350
fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {

tests/ui/issues/issue-34373.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ trait Trait<T> {
66

77
pub struct Foo<T = Box<Trait<DefaultFoo>>>; //~ ERROR cycle detected
88
//~^ ERROR `T` is never used
9-
//~| ERROR `Trait` cannot be made into an object
109
type DefaultFoo = Foo;
1110

1211
fn main() {

tests/ui/issues/issue-34373.stderr

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | pub struct Foo<T = Box<Trait<DefaultFoo>>>;
55
| ^^^^^^^^^^
66
|
77
note: ...which requires expanding type alias `DefaultFoo`...
8-
--> $DIR/issue-34373.rs:10:19
8+
--> $DIR/issue-34373.rs:9:19
99
|
1010
LL | type DefaultFoo = Foo;
1111
| ^^^
@@ -17,28 +17,6 @@ LL | pub struct Foo<T = Box<Trait<DefaultFoo>>>;
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818
= 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
1919

20-
error[E0038]: the trait `Trait` cannot be made into an object
21-
--> $DIR/issue-34373.rs:7:24
22-
|
23-
LL | pub struct Foo<T = Box<Trait<DefaultFoo>>>;
24-
| ^^^^^^^^^^^^^^^^^ `Trait` cannot be made into an object
25-
|
26-
note: for a trait to be "object safe" 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>
27-
--> $DIR/issue-34373.rs:4:8
28-
|
29-
LL | trait Trait<T> {
30-
| ----- this trait cannot be made into an object...
31-
LL | fn foo(_: T) {}
32-
| ^^^ ...because associated function `foo` has no `self` parameter
33-
help: consider turning `foo` into a method by giving it a `&self` argument
34-
|
35-
LL | fn foo(&self, _: T) {}
36-
| ++++++
37-
help: alternatively, consider constraining `foo` so it does not apply to trait objects
38-
|
39-
LL | fn foo(_: T) where Self: Sized {}
40-
| +++++++++++++++++
41-
4220
error[E0392]: type parameter `T` is never used
4321
--> $DIR/issue-34373.rs:7:16
4422
|
@@ -48,7 +26,7 @@ LL | pub struct Foo<T = Box<Trait<DefaultFoo>>>;
4826
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
4927
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
5028

51-
error: aborting due to 3 previous errors
29+
error: aborting due to 2 previous errors
5230

53-
Some errors have detailed explanations: E0038, E0391, E0392.
54-
For more information about an error, try `rustc --explain E0038`.
31+
Some errors have detailed explanations: E0391, E0392.
32+
For more information about an error, try `rustc --explain E0391`.

tests/ui/traits/issue-106072.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#[derive(Clone)] //~ trait objects must include the `dyn` keyword
2-
//~^ ERROR: the size for values of type `(dyn Foo + 'static)` cannot be known
3-
//~| ERROR: return type cannot have an unboxed trait object
2+
//~^ ERROR: return type cannot have an unboxed trait object
43
struct Foo;
54
trait Foo {} //~ the name `Foo` is defined multiple times
65
fn main() {}

tests/ui/traits/issue-106072.stderr

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0428]: the name `Foo` is defined multiple times
2-
--> $DIR/issue-106072.rs:5:1
2+
--> $DIR/issue-106072.rs:4:1
33
|
44
LL | struct Foo;
55
| ----------- previous definition of the type `Foo` here
@@ -8,17 +8,6 @@ LL | trait Foo {}
88
|
99
= note: `Foo` must be defined only once in the type namespace of this module
1010

11-
error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time
12-
--> $DIR/issue-106072.rs:1:10
13-
|
14-
LL | #[derive(Clone)]
15-
| ^^^^^ doesn't have a size known at compile-time
16-
|
17-
= help: the trait `Sized` is not implemented for `(dyn Foo + 'static)`
18-
note: required by a bound in `Clone`
19-
--> $SRC_DIR/core/src/clone.rs:LL:COL
20-
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
21-
2211
error[E0746]: return type cannot have an unboxed trait object
2312
--> $DIR/issue-106072.rs:1:10
2413
|
@@ -35,7 +24,7 @@ LL | #[derive(Clone)]
3524
|
3625
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
3726

38-
error: aborting due to 4 previous errors
27+
error: aborting due to 3 previous errors
3928

40-
Some errors have detailed explanations: E0277, E0428, E0746, E0782.
41-
For more information about an error, try `rustc --explain E0277`.
29+
Some errors have detailed explanations: E0428, E0746, E0782.
30+
For more information about an error, try `rustc --explain E0428`.

tests/ui/typeof/type_mismatch.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ fn main() {
44
const a: u8 = 1;
55
let b: typeof(a) = 1i8;
66
//~^ ERROR `typeof` is a reserved keyword but unimplemented
7-
//~| ERROR mismatched types
8-
//~| expected `u8`, found `i8`
97
}

tests/ui/typeof/type_mismatch.stderr

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,6 @@ help: consider replacing `typeof(...)` with an actual type
99
LL | let b: u8 = 1i8;
1010
| ~~
1111

12-
error[E0308]: mismatched types
13-
--> $DIR/type_mismatch.rs:5:24
14-
|
15-
LL | let b: typeof(a) = 1i8;
16-
| --------- ^^^ expected `u8`, found `i8`
17-
| |
18-
| expected due to this
19-
|
20-
help: change the type of the numeric literal from `i8` to `u8`
21-
|
22-
LL | let b: typeof(a) = 1u8;
23-
| ~~
24-
25-
error: aborting due to 2 previous errors
12+
error: aborting due to 1 previous error
2613

27-
Some errors have detailed explanations: E0308, E0516.
28-
For more information about an error, try `rustc --explain E0308`.
14+
For more information about this error, try `rustc --explain E0516`.

tests/ui/wf/ice-hir-wf-check-anon-const-issue-122199.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ trait Trait<const N: Trait = bar> {
22
//~^ ERROR cannot find value `bar` in this scope
33
//~| ERROR cycle detected when computing type of `Trait::N`
44
//~| ERROR cycle detected when computing type of `Trait::N`
5-
//~| ERROR the trait `Trait` cannot be made into an object
6-
//~| ERROR the trait `Trait` cannot be made into an object
7-
//~| ERROR the trait `Trait` cannot be made into an object
8-
//~| ERROR `(dyn Trait<{const error}> + 'static)` is forbidden as the type of a const generic parameter
9-
//~| WARN trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
10-
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
115
//~| WARN trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
126
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
137
fn fnc<const N: Trait = u32>(&self) -> Trait {
@@ -23,6 +17,7 @@ trait Trait<const N: Trait = bar> {
2317
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2418
//~| WARN trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
2519
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
20+
//~| ERROR `Trait` cannot be made into an object
2621
bar
2722
//~^ ERROR cannot find value `bar` in this scope
2823
}

0 commit comments

Comments
 (0)