Skip to content

Commit

Permalink
Detect duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jun 5, 2024
1 parent f3e5332 commit 9ca8fb5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
7 changes: 5 additions & 2 deletions compiler/rustc_ast_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ ast_passes_assoc_type_without_body =
associated type in `impl` without body
.suggestion = provide a definition for the type
ast_passes_precise_capturing_not_allowed_here = `use<...>` precise capturing syntax not allowed in {$loc}
ast_passes_at_least_one_trait = at least one trait must be specified
ast_passes_auto_generic = auto traits cannot have generic parameters
Expand Down Expand Up @@ -212,6 +210,11 @@ ast_passes_pattern_in_fn_pointer = patterns aren't allowed in function pointer t
ast_passes_pattern_in_foreign = patterns aren't allowed in foreign function declarations
.label = pattern not allowed in foreign function
ast_passes_precise_capturing_duplicated = duplicate `use<...>` precise capturing syntax
.label = second `use<...>` here
ast_passes_precise_capturing_not_allowed_here = `use<...>` precise capturing syntax not allowed in {$loc}
ast_passes_show_span = {$msg}
ast_passes_stability_outside_std = stability attributes may not be used outside of the standard library
Expand Down
20 changes: 18 additions & 2 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,24 @@ impl<'a> AstValidator<'a> {
// Mirrors `visit::walk_ty`, but tracks relevant state.
fn walk_ty(&mut self, t: &'a Ty) {
match &t.kind {
TyKind::ImplTrait(..) => {
self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t))
TyKind::ImplTrait(_, bounds) => {
self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t));

// FIXME(precise_capturing): If we were to allow `use` in other positions
// (e.g. GATs), then we must validate those as well. However, we don't have
// a good way of doing this with the current `Visitor` structure.
let mut use_bounds = bounds
.iter()
.filter_map(|bound| match bound {
GenericBound::Use(_, span) => Some(span),
_ => None,
})
.copied();
if let Some(bound1) = use_bounds.next()
&& let Some(bound2) = use_bounds.next()
{
self.dcx().emit_err(errors::DuplicatePreciseCapturing { bound1, bound2 });
}
}
TyKind::TraitObject(..) => self
.with_tilde_const(Some(DisallowTildeConstContext::TraitObject), |this| {
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,3 +836,12 @@ pub struct PreciseCapturingNotAllowedHere {
pub span: Span,
pub loc: &'static str,
}

#[derive(Diagnostic)]
#[diag(ast_passes_precise_capturing_duplicated)]
pub struct DuplicatePreciseCapturing {
#[primary_span]
pub bound1: Span,
#[label]
pub bound2: Span,
}
7 changes: 7 additions & 0 deletions tests/ui/impl-trait/precise-capturing/duplicated-use.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(precise_capturing)]
//~^ WARN the feature `precise_capturing` is incomplete

fn hello<'a>() -> impl Sized + use<'a> + use<'a> {}
//~^ ERROR duplicate `use<...>` precise capturing syntax

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/impl-trait/precise-capturing/duplicated-use.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: duplicate `use<...>` precise capturing syntax
--> $DIR/duplicated-use.rs:4:32
|
LL | fn hello<'a>() -> impl Sized + use<'a> + use<'a> {}
| ^^^^^^^ ------- second `use<...>` here

warning: the feature `precise_capturing` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/duplicated-use.rs:1:12
|
LL | #![feature(precise_capturing)]
| ^^^^^^^^^^^^^^^^^
|
= note: see issue #123432 <https://github.com/rust-lang/rust/issues/123432> for more information
= note: `#[warn(incomplete_features)]` on by default

error: aborting due to 1 previous error; 1 warning emitted

0 comments on commit 9ca8fb5

Please sign in to comment.