@@ -46,7 +46,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4646use rustc_data_structures:: sorted_map:: SortedMap ;
4747use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
4848use rustc_data_structures:: sync:: Lrc ;
49- use rustc_errors:: struct_span_err;
49+ use rustc_errors:: { struct_span_err, Applicability } ;
5050use rustc_hir as hir;
5151use rustc_hir:: def:: { DefKind , Namespace , PartialRes , PerNS , Res } ;
5252use rustc_hir:: def_id:: { DefId , DefPathHash , LocalDefId , CRATE_DEF_ID } ;
@@ -253,7 +253,7 @@ enum ImplTraitContext {
253253 /// equivalent to a fresh universal parameter like `fn foo<T: Debug>(x: T)`.
254254 ///
255255 /// Newly generated parameters should be inserted into the given `Vec`.
256- Universal ( LocalDefId ) ,
256+ Universal ,
257257
258258 /// Treat `impl Trait` as shorthand for a new opaque type.
259259 /// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
@@ -857,20 +857,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
857857 itctx : ImplTraitContext ,
858858 ) -> hir:: TypeBinding < ' hir > {
859859 debug ! ( "lower_assoc_ty_constraint(constraint={:?}, itctx={:?})" , constraint, itctx) ;
860-
861860 // lower generic arguments of identifier in constraint
862861 let gen_args = if let Some ( ref gen_args) = constraint. gen_args {
863862 let gen_args_ctor = match gen_args {
864863 GenericArgs :: AngleBracketed ( ref data) => {
865864 self . lower_angle_bracketed_parameter_data ( data, ParamMode :: Explicit , itctx) . 0
866865 }
867866 GenericArgs :: Parenthesized ( ref data) => {
868- let mut err = self . sess . struct_span_err (
869- gen_args. span ( ) ,
870- "parenthesized generic arguments cannot be used in associated type constraints"
871- ) ;
872- // FIXME: try to write a suggestion here
873- err. emit ( ) ;
867+ self . assoc_ty_contraint_param_error_emit ( data) ;
874868 self . lower_angle_bracketed_parameter_data (
875869 & data. as_angle_bracketed_args ( ) ,
876870 ParamMode :: Explicit ,
@@ -893,7 +887,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
893887 hir:: TypeBindingKind :: Equality { term }
894888 }
895889 AssocConstraintKind :: Bound { ref bounds } => {
896- let mut parent_def_id = self . current_hir_id_owner ;
897890 // Piggy-back on the `impl Trait` context to figure out the correct behavior.
898891 let ( desugar_to_impl_trait, itctx) = match itctx {
899892 // We are in the return position:
@@ -913,10 +906,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
913906 // so desugar to
914907 //
915908 // fn foo(x: dyn Iterator<Item = impl Debug>)
916- ImplTraitContext :: Universal ( parent) if self . is_in_dyn_type => {
917- parent_def_id = parent;
918- ( true , itctx)
919- }
909+ ImplTraitContext :: Universal if self . is_in_dyn_type => ( true , itctx) ,
920910
921911 // In `type Foo = dyn Iterator<Item: Debug>` we desugar to
922912 // `type Foo = dyn Iterator<Item = impl Debug>` but we have to override the
@@ -942,6 +932,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
942932 // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by
943933 // constructing the HIR for `impl bounds...` and then lowering that.
944934
935+ let parent_def_id = self . current_hir_id_owner ;
945936 let impl_trait_node_id = self . resolver . next_node_id ( ) ;
946937 self . resolver . create_def (
947938 parent_def_id,
@@ -984,6 +975,42 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
984975 }
985976 }
986977
978+ fn assoc_ty_contraint_param_error_emit ( & self , data : & ParenthesizedArgs ) -> ( ) {
979+ let mut err = self . sess . struct_span_err (
980+ data. span ,
981+ "parenthesized generic arguments cannot be used in associated type constraints" ,
982+ ) ;
983+ // Suggest removing empty parentheses: "Trait()" -> "Trait"
984+ if data. inputs . is_empty ( ) {
985+ let parentheses_span =
986+ data. inputs_span . shrink_to_lo ( ) . to ( data. inputs_span . shrink_to_hi ( ) ) ;
987+ err. multipart_suggestion (
988+ "remove these parentheses" ,
989+ vec ! [ ( parentheses_span, String :: new( ) ) ] ,
990+ Applicability :: MaybeIncorrect ,
991+ ) ;
992+ }
993+ // Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
994+ else {
995+ // Start of parameters to the 1st argument
996+ let open_param = data. inputs_span . shrink_to_lo ( ) . to ( data
997+ . inputs
998+ . first ( )
999+ . unwrap ( )
1000+ . span
1001+ . shrink_to_lo ( ) ) ;
1002+ // End of last argument to end of parameters
1003+ let close_param =
1004+ data. inputs . last ( ) . unwrap ( ) . span . shrink_to_hi ( ) . to ( data. inputs_span . shrink_to_hi ( ) ) ;
1005+ err. multipart_suggestion (
1006+ & format ! ( "use angle brackets instead" , ) ,
1007+ vec ! [ ( open_param, String :: from( "<" ) ) , ( close_param, String :: from( ">" ) ) ] ,
1008+ Applicability :: MaybeIncorrect ,
1009+ ) ;
1010+ }
1011+ err. emit ( ) ;
1012+ }
1013+
9871014 fn lower_generic_arg (
9881015 & mut self ,
9891016 arg : & ast:: GenericArg ,
@@ -1184,12 +1211,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11841211 |this| this. lower_param_bounds ( bounds, nested_itctx) ,
11851212 )
11861213 }
1187- ImplTraitContext :: Universal ( parent_def_id ) => {
1214+ ImplTraitContext :: Universal => {
11881215 // Add a definition for the in-band `Param`.
11891216 let def_id = self . resolver . local_def_id ( def_node_id) ;
11901217
1191- let hir_bounds = self
1192- . lower_param_bounds ( bounds, ImplTraitContext :: Universal ( parent_def_id ) ) ;
1218+ let hir_bounds =
1219+ self . lower_param_bounds ( bounds, ImplTraitContext :: Universal ) ;
11931220 // Set the name to `impl Bound1 + Bound2`.
11941221 let ident = Ident :: from_str_and_span ( & pprust:: ty_to_string ( t) , span) ;
11951222 let param = hir:: GenericParam {
@@ -1399,10 +1426,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13991426 }
14001427 let inputs = self . arena . alloc_from_iter ( inputs. iter ( ) . map ( |param| {
14011428 if fn_node_id. is_some ( ) {
1402- self . lower_ty_direct (
1403- & param. ty ,
1404- ImplTraitContext :: Universal ( self . current_hir_id_owner ) ,
1405- )
1429+ self . lower_ty_direct ( & param. ty , ImplTraitContext :: Universal )
14061430 } else {
14071431 self . lower_ty_direct (
14081432 & param. ty ,
0 commit comments