@@ -5404,125 +5404,125 @@ void attribClass(ClassSymbol c) throws CompletionFailure {
54045404 // Get environment current at the point of class definition.
54055405 Env <AttrContext > env = typeEnvs .get (c );
54065406
5407- if ( c . isSealed () &&
5408- ! c . isEnum () &&
5409- ! c . isPermittedExplicit &&
5410- c . permitted . isEmpty ()) {
5411- log . error ( TreeInfo . diagnosticPositionFor ( c , env . tree ), Errors . SealedClassMustHaveSubclasses );
5412- }
5413-
5414- if ( c . isSealed ()) {
5415- Set < Symbol > permittedTypes = new HashSet <>();
5416- boolean sealedInUnnamed = c . packge (). modle == syms . unnamedModule || c . packge (). modle == syms . noModule ;
5417- for ( Symbol subTypeSym : c . permitted ) {
5418- boolean isTypeVar = false ;
5419- if ( subTypeSym . type . getTag () == TYPEVAR ) {
5420- isTypeVar = true ; //error recovery
5421- log . error ( TreeInfo . diagnosticPositionFor ( subTypeSym , env .tree ),
5422- Errors . InvalidPermitsClause ( Fragments . IsATypeVariable ( subTypeSym . type )));
5423- }
5424- if (subTypeSym . isAnonymous () && ! c . isEnum ()) {
5425- log . error ( TreeInfo . diagnosticPositionFor ( subTypeSym , env . tree ), Errors . LocalClassesCantExtendSealed ( Fragments . Anonymous ));
5426- }
5427- if ( permittedTypes . contains ( subTypeSym )) {
5428- DiagnosticPosition pos =
5429- env . enclClass . permitting . stream ()
5430- . filter ( permittedExpr -> TreeInfo . diagnosticPositionFor ( subTypeSym , permittedExpr , true ) != null )
5431- . limit ( 2 ). collect ( List . collector ()). get ( 1 );
5432- log . error ( pos , Errors . InvalidPermitsClause ( Fragments . IsDuplicated ( subTypeSym . type )) );
5433- } else {
5434- permittedTypes . add ( subTypeSym );
5435- }
5436- if (sealedInUnnamed ) {
5437- if ( subTypeSym . packge () != c . packge ()) {
5407+ // The info.lint field in the envs stored in typeEnvs is deliberately uninitialized,
5408+ // because the annotations were not available at the time the env was created. Therefore,
5409+ // we look up the environment chain for the first enclosing environment for which the
5410+ // lint value is set. Typically, this is the parent env, but might be further if there
5411+ // are any envs created as a result of TypeParameter nodes.
5412+ Env < AttrContext > lintEnv = env ;
5413+ while ( lintEnv . info . lint == null )
5414+ lintEnv = lintEnv . next ;
5415+
5416+ // Having found the enclosing lint value, we can initialize the lint value for this class
5417+ env . info . lint = lintEnv . info . lint . augment ( c );
5418+
5419+ Lint prevLint = chk . setLint ( env . info . lint );
5420+ JavaFileObject prev = log . useSource ( c . sourcefile );
5421+ ResultInfo prevReturnRes = env .info . returnResult ;
5422+
5423+ try {
5424+ if (c . isSealed () &&
5425+ ! c . isEnum () &&
5426+ ! c . isPermittedExplicit &&
5427+ c . permitted . isEmpty ( )) {
5428+ log . error ( TreeInfo . diagnosticPositionFor ( c , env . tree ), Errors . SealedClassMustHaveSubclasses );
5429+ }
5430+
5431+ if ( c . isSealed ()) {
5432+ Set < Symbol > permittedTypes = new HashSet <>( );
5433+ boolean sealedInUnnamed = c . packge (). modle == syms . unnamedModule || c . packge (). modle == syms . noModule ;
5434+ for ( Symbol subTypeSym : c . permitted ) {
5435+ boolean isTypeVar = false ;
5436+ if (subTypeSym . type . getTag () == TYPEVAR ) {
5437+ isTypeVar = true ; //error recovery
54385438 log .error (TreeInfo .diagnosticPositionFor (subTypeSym , env .tree ),
5439- Errors .ClassInUnnamedModuleCantExtendSealedInDiffPackage (c )
5440- );
5439+ Errors .InvalidPermitsClause (Fragments .IsATypeVariable (subTypeSym .type )));
54415440 }
5442- } else if (subTypeSym .packge ().modle != c .packge ().modle ) {
5443- log .error (TreeInfo .diagnosticPositionFor (subTypeSym , env .tree ),
5444- Errors .ClassInModuleCantExtendSealedInDiffModule (c , c .packge ().modle )
5445- );
5446- }
5447- if (subTypeSym == c .type .tsym || types .isSuperType (subTypeSym .type , c .type )) {
5448- log .error (TreeInfo .diagnosticPositionFor (subTypeSym , ((JCClassDecl )env .tree ).permitting ),
5449- Errors .InvalidPermitsClause (
5450- subTypeSym == c .type .tsym ?
5451- Fragments .MustNotBeSameClass :
5452- Fragments .MustNotBeSupertype (subTypeSym .type )
5453- )
5454- );
5455- } else if (!isTypeVar ) {
5456- boolean thisIsASuper = types .directSupertypes (subTypeSym .type )
5457- .stream ()
5458- .anyMatch (d -> d .tsym == c );
5459- if (!thisIsASuper ) {
5441+ if (subTypeSym .isAnonymous () && !c .isEnum ()) {
5442+ log .error (TreeInfo .diagnosticPositionFor (subTypeSym , env .tree ), Errors .LocalClassesCantExtendSealed (Fragments .Anonymous ));
5443+ }
5444+ if (permittedTypes .contains (subTypeSym )) {
5445+ DiagnosticPosition pos =
5446+ env .enclClass .permitting .stream ()
5447+ .filter (permittedExpr -> TreeInfo .diagnosticPositionFor (subTypeSym , permittedExpr , true ) != null )
5448+ .limit (2 ).collect (List .collector ()).get (1 );
5449+ log .error (pos , Errors .InvalidPermitsClause (Fragments .IsDuplicated (subTypeSym .type )));
5450+ } else {
5451+ permittedTypes .add (subTypeSym );
5452+ }
5453+ if (sealedInUnnamed ) {
5454+ if (subTypeSym .packge () != c .packge ()) {
5455+ log .error (TreeInfo .diagnosticPositionFor (subTypeSym , env .tree ),
5456+ Errors .ClassInUnnamedModuleCantExtendSealedInDiffPackage (c )
5457+ );
5458+ }
5459+ } else if (subTypeSym .packge ().modle != c .packge ().modle ) {
54605460 log .error (TreeInfo .diagnosticPositionFor (subTypeSym , env .tree ),
5461- Errors .InvalidPermitsClause (Fragments .DoesntExtendSealed (subTypeSym .type )));
5461+ Errors .ClassInModuleCantExtendSealedInDiffModule (c , c .packge ().modle )
5462+ );
5463+ }
5464+ if (subTypeSym == c .type .tsym || types .isSuperType (subTypeSym .type , c .type )) {
5465+ log .error (TreeInfo .diagnosticPositionFor (subTypeSym , ((JCClassDecl )env .tree ).permitting ),
5466+ Errors .InvalidPermitsClause (
5467+ subTypeSym == c .type .tsym ?
5468+ Fragments .MustNotBeSameClass :
5469+ Fragments .MustNotBeSupertype (subTypeSym .type )
5470+ )
5471+ );
5472+ } else if (!isTypeVar ) {
5473+ boolean thisIsASuper = types .directSupertypes (subTypeSym .type )
5474+ .stream ()
5475+ .anyMatch (d -> d .tsym == c );
5476+ if (!thisIsASuper ) {
5477+ log .error (TreeInfo .diagnosticPositionFor (subTypeSym , env .tree ),
5478+ Errors .InvalidPermitsClause (Fragments .DoesntExtendSealed (subTypeSym .type )));
5479+ }
54625480 }
54635481 }
54645482 }
5465- }
54665483
5467- List <ClassSymbol > sealedSupers = types .directSupertypes (c .type )
5468- .stream ()
5469- .filter (s -> s .tsym .isSealed ())
5470- .map (s -> (ClassSymbol ) s .tsym )
5471- .collect (List .collector ());
5484+ List <ClassSymbol > sealedSupers = types .directSupertypes (c .type )
5485+ .stream ()
5486+ .filter (s -> s .tsym .isSealed ())
5487+ .map (s -> (ClassSymbol ) s .tsym )
5488+ .collect (List .collector ());
54725489
5473- if (sealedSupers .isEmpty ()) {
5474- if ((c .flags_field & Flags .NON_SEALED ) != 0 ) {
5475- boolean hasErrorSuper = false ;
5490+ if (sealedSupers .isEmpty ()) {
5491+ if ((c .flags_field & Flags .NON_SEALED ) != 0 ) {
5492+ boolean hasErrorSuper = false ;
54765493
5477- hasErrorSuper |= types .directSupertypes (c .type )
5478- .stream ()
5479- .anyMatch (s -> s .tsym .kind == Kind .ERR );
5494+ hasErrorSuper |= types .directSupertypes (c .type )
5495+ .stream ()
5496+ .anyMatch (s -> s .tsym .kind == Kind .ERR );
54805497
5481- ClassType ct = (ClassType ) c .type ;
5498+ ClassType ct = (ClassType ) c .type ;
54825499
5483- hasErrorSuper |= !ct .isCompound () && ct .interfaces_field != ct .all_interfaces_field ;
5500+ hasErrorSuper |= !ct .isCompound () && ct .interfaces_field != ct .all_interfaces_field ;
54845501
5485- if (!hasErrorSuper ) {
5486- log .error (TreeInfo .diagnosticPositionFor (c , env .tree ), Errors .NonSealedWithNoSealedSupertype (c ));
5502+ if (!hasErrorSuper ) {
5503+ log .error (TreeInfo .diagnosticPositionFor (c , env .tree ), Errors .NonSealedWithNoSealedSupertype (c ));
5504+ }
5505+ }
5506+ } else {
5507+ if (c .isDirectlyOrIndirectlyLocal () && !c .isEnum ()) {
5508+ log .error (TreeInfo .diagnosticPositionFor (c , env .tree ), Errors .LocalClassesCantExtendSealed (c .isAnonymous () ? Fragments .Anonymous : Fragments .Local ));
54875509 }
5488- }
5489- } else {
5490- if (c .isDirectlyOrIndirectlyLocal () && !c .isEnum ()) {
5491- log .error (TreeInfo .diagnosticPositionFor (c , env .tree ), Errors .LocalClassesCantExtendSealed (c .isAnonymous () ? Fragments .Anonymous : Fragments .Local ));
5492- }
54935510
5494- if (!c .type .isCompound ()) {
5495- for (ClassSymbol supertypeSym : sealedSupers ) {
5496- if (!supertypeSym .permitted .contains (c .type .tsym )) {
5497- log .error (TreeInfo .diagnosticPositionFor (c .type .tsym , env .tree ), Errors .CantInheritFromSealed (supertypeSym ));
5511+ if (!c .type .isCompound ()) {
5512+ for (ClassSymbol supertypeSym : sealedSupers ) {
5513+ if (!supertypeSym .permitted .contains (c .type .tsym )) {
5514+ log .error (TreeInfo .diagnosticPositionFor (c .type .tsym , env .tree ), Errors .CantInheritFromSealed (supertypeSym ));
5515+ }
5516+ }
5517+ if (!c .isNonSealed () && !c .isFinal () && !c .isSealed ()) {
5518+ log .error (TreeInfo .diagnosticPositionFor (c , env .tree ),
5519+ c .isInterface () ?
5520+ Errors .NonSealedOrSealedExpected :
5521+ Errors .NonSealedSealedOrFinalExpected );
54985522 }
5499- }
5500- if (!c .isNonSealed () && !c .isFinal () && !c .isSealed ()) {
5501- log .error (TreeInfo .diagnosticPositionFor (c , env .tree ),
5502- c .isInterface () ?
5503- Errors .NonSealedOrSealedExpected :
5504- Errors .NonSealedSealedOrFinalExpected );
55055523 }
55065524 }
5507- }
55085525
5509- // The info.lint field in the envs stored in typeEnvs is deliberately uninitialized,
5510- // because the annotations were not available at the time the env was created. Therefore,
5511- // we look up the environment chain for the first enclosing environment for which the
5512- // lint value is set. Typically, this is the parent env, but might be further if there
5513- // are any envs created as a result of TypeParameter nodes.
5514- Env <AttrContext > lintEnv = env ;
5515- while (lintEnv .info .lint == null )
5516- lintEnv = lintEnv .next ;
5517-
5518- // Having found the enclosing lint value, we can initialize the lint value for this class
5519- env .info .lint = lintEnv .info .lint .augment (c );
5520-
5521- Lint prevLint = chk .setLint (env .info .lint );
5522- JavaFileObject prev = log .useSource (c .sourcefile );
5523- ResultInfo prevReturnRes = env .info .returnResult ;
5524-
5525- try {
55265526 deferredLintHandler .flush (env .tree );
55275527 env .info .returnResult = null ;
55285528 // java.lang.Enum may not be subclassed by a non-enum
0 commit comments