Skip to content

Commit df4ca44

Browse files
committed
Auto merge of rust-lang#132237 - matthiaskrgr:rollup-ulogwtd, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - rust-lang#132043 (Simplify param handling in `resolve_bound_vars`) - rust-lang#132214 (Cleanup: Move an impl-Trait check from AST validation to AST lowering) - rust-lang#132221 (Clean up some comments on lint implementation) - rust-lang#132228 (Revert "ci update freebsd version proposal, freebsd 12 being eol.") - rust-lang#132234 (Miri subtree update) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 81d6652 + 0cace65 commit df4ca44

Some content is hidden

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

60 files changed

+802
-616
lines changed

compiler/rustc_ast_lowering/src/path.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
3434
modifiers: Option<ast::TraitBoundModifiers>,
3535
) -> hir::QPath<'hir> {
3636
let qself_position = qself.as_ref().map(|q| q.position);
37-
let qself = qself.as_ref().map(|q| self.lower_ty(&q.ty, itctx));
37+
let qself = qself
38+
.as_ref()
39+
// Reject cases like `<impl Trait>::Assoc` and `<impl Trait as Trait>::Assoc`.
40+
.map(|q| self.lower_ty(&q.ty, ImplTraitContext::Disallowed(ImplTraitPosition::Path)));
3841

3942
let partial_res =
4043
self.resolver.get_partial_res(id).unwrap_or_else(|| PartialRes::new(Res::Err));
@@ -75,6 +78,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
7578
None
7679
};
7780

81+
// Only permit `impl Trait` in the final segment. E.g., we permit `Option<impl Trait>`,
82+
// `option::Option<T>::Xyz<impl Trait>` and reject `option::Option<impl Trait>::Xyz`.
83+
let itctx = |i| {
84+
if i + 1 == p.segments.len() {
85+
itctx
86+
} else {
87+
ImplTraitContext::Disallowed(ImplTraitPosition::Path)
88+
}
89+
};
90+
7891
let path_span_lo = p.span.shrink_to_lo();
7992
let proj_start = p.segments.len() - unresolved_segments;
8093
let path = self.arena.alloc(hir::Path {
@@ -121,7 +134,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
121134
segment,
122135
param_mode,
123136
generic_args_mode,
124-
itctx,
137+
itctx(i),
125138
bound_modifier_allowed_features.clone(),
126139
)
127140
},
@@ -185,7 +198,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
185198
segment,
186199
param_mode,
187200
generic_args_mode,
188-
itctx,
201+
itctx(i),
189202
None,
190203
));
191204
let qpath = hir::QPath::TypeRelative(ty, hir_segment);

compiler/rustc_ast_passes/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ ast_passes_generic_before_constraints = generic arguments must come before the f
146146
147147
ast_passes_generic_default_trailing = generic parameters with a default must be trailing
148148
149-
ast_passes_impl_trait_path = `impl Trait` is not allowed in path parameters
150-
151149
ast_passes_incompatible_features = `{$f1}` and `{$f2}` are incompatible, using them at the same time is not allowed
152150
.help = remove one of these features
153151

compiler/rustc_ast_passes/src/ast_validation.rs

-46
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ struct AstValidator<'a> {
8080

8181
disallow_tilde_const: Option<TildeConstReason>,
8282

83-
/// Used to ban `impl Trait` in path projections like `<impl Iterator>::Item`
84-
/// or `Foo::Bar<impl Trait>`
85-
is_impl_trait_banned: bool,
86-
8783
/// Used to ban explicit safety on foreign items when the extern block is not marked as unsafe.
8884
extern_mod_safety: Option<Safety>,
8985

@@ -123,12 +119,6 @@ impl<'a> AstValidator<'a> {
123119
self.extern_mod_safety = old;
124120
}
125121

126-
fn with_banned_impl_trait(&mut self, f: impl FnOnce(&mut Self)) {
127-
let old = mem::replace(&mut self.is_impl_trait_banned, true);
128-
f(self);
129-
self.is_impl_trait_banned = old;
130-
}
131-
132122
fn with_tilde_const(
133123
&mut self,
134124
disallowed: Option<TildeConstReason>,
@@ -213,37 +203,6 @@ impl<'a> AstValidator<'a> {
213203
.with_tilde_const(Some(TildeConstReason::TraitObject), |this| {
214204
visit::walk_ty(this, t)
215205
}),
216-
TyKind::Path(qself, path) => {
217-
// We allow these:
218-
// - `Option<impl Trait>`
219-
// - `option::Option<impl Trait>`
220-
// - `option::Option<T>::Foo<impl Trait>`
221-
//
222-
// But not these:
223-
// - `<impl Trait>::Foo`
224-
// - `option::Option<impl Trait>::Foo`.
225-
//
226-
// To implement this, we disallow `impl Trait` from `qself`
227-
// (for cases like `<impl Trait>::Foo>`)
228-
// but we allow `impl Trait` in `GenericArgs`
229-
// iff there are no more PathSegments.
230-
if let Some(qself) = qself {
231-
// `impl Trait` in `qself` is always illegal
232-
self.with_banned_impl_trait(|this| this.visit_ty(&qself.ty));
233-
}
234-
235-
// Note that there should be a call to visit_path here,
236-
// so if any logic is added to process `Path`s a call to it should be
237-
// added both in visit_path and here. This code mirrors visit::walk_path.
238-
for (i, segment) in path.segments.iter().enumerate() {
239-
// Allow `impl Trait` iff we're on the final path segment
240-
if i == path.segments.len() - 1 {
241-
self.visit_path_segment(segment);
242-
} else {
243-
self.with_banned_impl_trait(|this| this.visit_path_segment(segment));
244-
}
245-
}
246-
}
247206
_ => visit::walk_ty(self, t),
248207
}
249208
}
@@ -737,10 +696,6 @@ impl<'a> AstValidator<'a> {
737696
}
738697
}
739698
TyKind::ImplTrait(_, bounds) => {
740-
if self.is_impl_trait_banned {
741-
self.dcx().emit_err(errors::ImplTraitPath { span: ty.span });
742-
}
743-
744699
if let Some(outer_impl_trait_sp) = self.outer_impl_trait {
745700
self.dcx().emit_err(errors::NestedImplTrait {
746701
span: ty.span,
@@ -1729,7 +1684,6 @@ pub fn check_crate(
17291684
has_proc_macro_decls: false,
17301685
outer_impl_trait: None,
17311686
disallow_tilde_const: Some(TildeConstReason::Item),
1732-
is_impl_trait_banned: false,
17331687
extern_mod_safety: None,
17341688
lint_buffer: lints,
17351689
};

compiler/rustc_ast_passes/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -418,13 +418,6 @@ pub(crate) struct TraitObjectBound {
418418
pub span: Span,
419419
}
420420

421-
#[derive(Diagnostic)]
422-
#[diag(ast_passes_impl_trait_path, code = E0667)]
423-
pub(crate) struct ImplTraitPath {
424-
#[primary_span]
425-
pub span: Span,
426-
}
427-
428421
#[derive(Diagnostic)]
429422
#[diag(ast_passes_nested_impl_trait, code = E0666)]
430423
pub(crate) struct NestedImplTrait {

compiler/rustc_error_codes/src/error_codes/E0667.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
13
`impl Trait` is not allowed in path parameters.
24

35
Erroneous code example:
46

5-
```compile_fail,E0667
7+
```ignore (removed error code)
68
fn some_fn(mut x: impl Iterator) -> <impl Iterator>::Item { // error!
79
x.next().unwrap()
810
}
@@ -11,7 +13,7 @@ fn some_fn(mut x: impl Iterator) -> <impl Iterator>::Item { // error!
1113
You cannot use `impl Trait` in path parameters. If you want something
1214
equivalent, you can do this instead:
1315

14-
```
16+
```ignore (removed error code)
1517
fn some_fn<T: Iterator>(mut x: T) -> T::Item { // ok!
1618
x.next().unwrap()
1719
}

0 commit comments

Comments
 (0)