Commit Graph

91 Commits

Author SHA1 Message Date
Alex Hoppen
66104395d7 [Sema/SourceKit] Emit same diagnostics for missing protocol requirements on the command line and in SourceKit
Some editors use diagnostics from SourceKit to replace build issues. This causes issues if the diagnostics from SourceKit are formatted differently than the build issues. Make sure they are rendered the same way, removing most uses of `DiagnosticsEditorMode`.

To do so, always emit the `add stubs for conformance` note (which previously was only emitted in editor mode) and remove all `; add <something>` suffixes from notes that state which requirements are missing.

rdar://129283608
2024-08-07 14:01:30 -07:00
Alexis Laferrière
6d9779f8bb [Sema] Remove duplicated information when we can only use AccessScope
Stop relying on a separate access-level from the access-scope to show
and prioritize diagnostics. This change should be NFC but it does break some
tests. This only affects the prioritization of which of many offending type
is the worst and should be pointed to, the logic remains sound.
2023-09-13 15:40:33 -07:00
Slava Pestov
290701cb4d Sema: Ban shadowing generic parameters from outer scopes
Code like that is usually indicative of programmer error, and does not
round-trip through module interface files since there is no source
syntax to refer to an outer generic parameter.

For source compatibility this is a warning, but becomes an error with
-swift-version 6.

Fixes rdar://problem/108385980 and https://github.com/apple/swift/issues/62767.
2023-04-25 17:41:23 -04:00
Ellie Shin
772e42e448 Add a missing check for a corner case with package use site / internal decl site
Update tests

Resolves rdar://106732804, rdar://104987455
2023-03-28 16:59:20 -07:00
Ellie Shin
671533fed2 Associate PackageUnit with ModuleDecl
* Weakly reference ModuleDecl from PackageUnit
* Add PackageUnit decl context getter and use it for a package AccessScope
* Return module decl referenced by PackageUnit in getModuleScopeContext and getParentModule
* Handle package acl in access scope checkers
* Remove AccessLimitKind
* Fix tests
Resolves rdar://104987295, rdar://105187216, rdar://104723918
2023-03-10 16:19:32 -08:00
Alexis Laferrière
75255d153c [Sema] Diagnose exportability of decls limited by a non-public import
Check the API of decls to ensure that imported types are only used in
decls with compatible access-level. For example, error on using a type
import as internal in a public function, but allow it in internal
functions and lower.

This change raises errors on wrongful use of types limited by an import
and offers a note pointing to the import that did limit the imported
type.
2023-03-01 18:35:41 -08:00
Ellie Shin
3fdf2ad2a9 Add doc comments and rdars to tests 2023-02-02 18:50:39 -08:00
Ellie Shin
be20333a87 Add access level and scope checks to package types
Resolves rdar://104617227
2023-02-02 16:51:17 -08:00
Holly Borla
7f30f5b039 [RequirementMachine] Downgrade concrete type parameter diagnostics to a
warning.

These diagnostics are stricter in the RequirementMachine than in the GSB,
and there's code that relies on the more relaxed diagnostics in the source
compatibility suite. Downgrade these diagnostics to a warning using
warnUntilSwiftVersion(6).
2022-04-11 19:18:00 -07:00
Holly Borla
236e8e2b1d [RequirementMachine] Diagnose type parameters that are made concrete by a
same-type requirement.
2022-04-08 18:16:23 -07:00
Slava Pestov
4068c9d707 Pass -requirement-machine-{protocol,inferred}-signatures=verify in remaining tests that fail due to diagnostics 2022-04-01 23:55:34 -04:00
Slava Pestov
7bc50889f4 Sema: Check declaration attributes before checking members
This simplifies matters if checking an attribute adds members
to the nominal type or extension.
2020-07-08 21:38:34 -04:00
Owen Voorhees
9222dfdd46 [Sema] Fix protocol refinement access control message (#26855)
Previously, the protocol refinement access control error
would always assume a protocol was being refined. Change it
to instead refer to the appropriate declaration kind.

Resolves SR-9195
2019-08-29 17:47:24 -07:00
gregomni
92eb874e0e Insert access level fixits in front of 'override', if it's there, instead of
after. This is so that code produced by this fixit matches the same keyword
order as code produced by autocompleting a method decl in a new subclass.
2019-06-27 06:57:02 -07:00
Jordan Rose
883fc5b24a Check access control for the generic requirements of typealiases.
Also oops. This one was a little more involved because the requirements
on a generic typealias don't always carry a Type anymore; sometimes all
you have is the TypeRepr. That should still be okay in practice as long
as we don't start doing that for var/let, which can have part of a type
be inferred but not all of it.
2019-04-01 18:34:18 -07:00
Jordan Rose
b4e476e61f Check access control for the generic requirements of subscripts
Oops.
2019-04-01 18:28:33 -07:00
Ding Ye
0f493a68b3 [Sema] Improve diagnostics for access level of protocol witness in extension. (#22235)
If the access level of a protocol witness does not satisfies a requirement,
the compiler suggests marking it as the required level.  This is not suitable
when the witness is in an extension whose specified access level is less than
the required level, since the fixit fights with other warnings in this case.
This patch identifies such case and produces improved diagnostics.

Resolves: SR-9793
2019-02-08 09:31:01 -08:00
Jordan Rose
3c740e4f2e Don't fix access of an 'open' decl in a 'public' extension
This is reasonable to diagnose with a warning, but dropping the 'open'
down to 'public' isn't the right fix, because now it's not a valid
override. The declaration has to get moved to another extension instead,
or the extension has to not set a default access level.

This turned out to be a source compat issue because the same logic
that emits the fix-it also updates the access of the member, which
then resulted in "must be as accessible as the declaration it
overrides" in the /same/ build. It's not immediately clear what caused
this; probably something's just being validated in a different order
than it was before. The change makes sense either way.

Stepping back, it's weird that a warning would change how the compiler
saw the code, and while we could check for 'override' explicitly, we
can't know if the member might be satisfying a protocol requirement.
Better to just not guess at the right answer here.

rdar://problem/47557376&28493971
2019-01-28 18:25:06 -08:00
Jordan Rose
1f06cd7e6d Tweak diagnostic for a high-access member in a low-access extension
Before: declaring a public instance method in a private extension
After: 'public' modifier conflicts with extension's default access of
       'private'
2019-01-28 18:24:12 -08:00
Jordan Rose
45eac2cffb Fix a hole in access control checking for var/let
Previously if a declaration had both a Type and a TypeRepr available,
we would only check the access of the TypeRepr. However, this is
incomplete when the type is partially inferred, as in

  public var inferredGenericParameters: Optional = PrivateStruct()

The new algorithm is to the Type first, then:
- if the Type is okay, move on to check the TypeRepr
- if the Type is not okay and we're in pre-Swift-5 mode, check the
  TypeRepr, and if /that's/ okay downgrade the whole thing to a
  warning.

Unfortunately, we can't /just/ check the Type in the "good" case,
because we don't always properly preserve sugar when going from a
TypeRepr that represents a typealias to the corresponding Type, and we
want to be able to fix those cases in the future. So we have to check
both.
2018-12-08 22:38:16 -08:00
Jordan Rose
5dace224a0 Merge pull request #18623 from dingobye/sr8453
[Sema] Warn for redundant access-level modifiers on setters or used in an extension.
2018-09-05 17:44:26 -07:00
Matt Diephouse
5e9da14b5a Pare down operator candidates during protocol type checking 2018-08-28 08:41:15 -04:00
Ding Ye
63e1937e44 Make it more comprehensive to warn when redundant access
modifier is used in an extension. In addition, add warnings
for access modifier redundancy on property setters; and
address comments from Jordan Rose.
2018-08-11 21:21:54 +10:00
Ding Ye
f34020bfd4 [Sema] Warn when redundant access-level modifier is added in an extension.
This patch adds warning for redundant access-level modifiers
used in an extension. It also refines the diagnostics of
access_control_ext_member_more issues, in case the fixit
could suggest redundant modifiers.

Resolves: SR-8453.
2018-08-10 14:33:16 +10:00
Jordan Rose
6290d9be60 Introduce DescriptiveDeclKind::Property (#18183)
...and collapse StaticVar/ClassVar and StaticLet/ClassLet into
StaticProperty/ClassProperty.

"var" and "let" aren't great nouns to use in diagnostics to begin with,
especially alongside semantic terms like "instance method". Focus on
the type vs. non-type aspect instead with "property", which better
matches how people talk about member vars (and lets) anyway.
2018-07-30 09:23:59 -07:00
Ding Ye
f735f97c64 [Sema] Fix inappropriate diagnostics of access control for members in private extensions. (#18105)
When a `fileprivate` method is declared in a `private`
extension, a warning is raised since access level
`fileprivate` is literally higher than `private`.
This is not appropriate because extensions are top level
declarations, for which `private` and `fileprivate` are
equivalent. This patch stops such warnings.

Resolves: SR-8306.
2018-07-20 13:46:57 -07:00
Slava Pestov
a7e51721ce Sema: Fix minor inconsistencies with access control diagnostics
The various 'isExplicit' checks were wrong -- they should apply
to protocol requirements only, not protocol extension members.

Also, we weren't performing this check for type aliases in
protocols, so you would get a misleading diagnostic telling you
the type alias must be declared private/internal, when in fact
type aliases in protocols cannot have their own access control.

Finally, the "should be declared <X>" diagnostics (the
!isExplicit case) need to state the most visible access level
that will work, and in a few cases we would say private instead
of fileprivate here.
2018-06-15 13:23:58 -07:00
Jordan Rose
6bd7e5e5b4 Make sure protocol witness errors don't leave the conformance context
That is, if there's a problem with a witness, and the witness comes
from a different extension from the conformance (or the original type,
when the conformance is on an extension), put the main diagnostic on
the conformance, with a note on the witness. This involves some
shuffling and rephrasing of existing diagnostics too.

There's a few reasons for this change:

- More context. It may not be obvious why a declaration in file
  A.swift needs to be marked 'public' if you can't see the conformance
  in B.swift.

- Better locations for imported declarations. If you're checking a
  conformance in a source file but the witness came from an imported
  module, it's better to put the diagnostic on the part you have
  control over. (This is especially true in Xcode, which can't display
  diagnostics on imported declarations in the source editor.)

- Plays better with batch mode. Without this change, you can have
  diagnostics being reported in file A.swift that are tied to a
  conformance declared in file B.swift. Of course the contents of
  A.swift also affect the diagnostic, but compiling A.swift on its
  own wouldn't produce the diagnostic, and so putting it there is
  problematic.

The change does in some cases make for a worse user experience,
though; if you just want to apply the changes and move on, the main
diagnostic isn't in the "right place". It's the note that has the info
and possible fix-it. It's also a slightly more complicated
implementation.
2018-05-10 19:31:12 -07:00
Anthony Latsis
79c0516133 [Diagnostics] SR-7349 Improve accessibility diagnostics for inheritance from generic classes (#16223)
* [Diagnostics] Improve accessibility diagnostics for inheritance from generic classes

Fixed misleading warning when message pointed to the type's superclass access level instead of the access level of the type used as a generic parameter of the superclass when inheriting

* updated 'Compatibility' tests (warnings)

* updated the diagnostic's error version

Resolves: SR-7349
2018-04-28 15:54:33 -07:00
Robert Widmann
03580d2fe5 Add a parameter list to EnumElementDecl
This models, but does not plumb through, default arguments.
2018-03-28 00:05:56 -04:00
Jordan Rose
d118e5c499 Allow 'public' classes to have 'internal' required initializers (#14775)
They're already not subclassable publicly, so it's okay for the
initializer to not be available to cross-module clients, just like if
it were non-'required'. This allows constructing a class instance
chosen at runtime /within/ the module without having to expose the
existence of the constructor to everybody.

rdar://problem/22845087
2018-02-26 14:08:24 -08:00
Jordan Rose
9888697544 Correctly check access control for a class's generic superclass (#12629)
The previous code was too clever in trying to avoid work and missed
the fact that ClassDecl::getSuperclass produces an interface type but
the types in the inheritance clause are contextual types.

This actually successfully built:

- in non-WMO builds with a public subclass and an internal base class,
  because the internal class symbol wouldn't get stripped out.

- in WMO builds with an internal subclass and a private base class,
  because 'private' has no distinction at the linkage level for a WMO
  build.

However, it's highly likely that trying to import a library containing
such types would result in instability (read: compiler and debugger
crashes), and it's clearly a mistake to allow this. (If you can't show
your superclass to a user in a library's generated interface,
something's definitely gone wrong.)

https://bugs.swift.org/browse/SR-6206
2017-11-09 13:29:24 -08:00
Doug Gregor
faf659d16b [Archetype builder] Make sure the archetype builder is always "finalized".
ArchetypeBuilder::finalize() is needed to tie up any loose ends before
requesting a generic signature or generic environment. Make sure it
gets called consistently.
2017-02-05 21:23:44 -08:00
Jacob Bandes-Storch
32635696bd [Diagnostics] handle ‘open’ access control; introduce %error modifier 2017-01-06 22:48:29 -08:00
Slava Pestov
9fba89bd7b Sema: Add simulation of buggy Swift 3 typealias accessibility checking
Basically if the underlying type of a typealias was dependent on
generic parameters from context, it wouldn't participate in
accessibility checking.

Turns out people were (accidentally) relying on this behavior, so
add a simulation of it in Swift 3 mode by ignoring such typealiases
entirely.

Fixes <rdar://problem/29549232>.
2016-12-12 14:44:01 -08:00
Doug Gregor
00a7981e05 [Type checker] Eliminate one "pass" in generic environment validation.
We no longer need a separate "pass" that creates an archetype builder
that inherits context archetypes, because we no longer ever inherit
context archetypes.
2016-12-07 14:27:49 -08:00
Slava Pestov
d1b753c46a Sema: Remove a workaround for an accessibility problem in the standard library 2016-12-04 23:57:11 -08:00
David Farler
b7d17b25ba Rename -parse flag to -typecheck
A parse-only option is needed for parse performance tracking and the
current option also includes semantic analysis.
2016-11-28 10:50:55 -08:00
Aleksey Gaponov
3419925ff1 [SR-2209] Fix access scope diagnostics for top-level declarations. 2016-11-09 21:18:37 +01:00
Jordan Rose
8b1ce7d1e7 Switch override checking over to access scopes. (#4508)
The class version of f65ad810d. Fixes some incorrect diagnostics.

We really do need https://bugs.swift.org/browse/SR-2209 to make
this all cleaner.

rdar://problem/27820665
2016-08-26 09:07:20 -07:00
Rintaro Ishizaki
9a0f9d1db9 [Sema] Minimum access for override method is fileprivate (#4404) 2016-08-19 13:11:22 -07:00
Jordan Rose
f65ad810df Check protocol witnesses using access scopes. (#4176)
...rather than relying on the access-as-spelled, which may be greater
than the effective access due to parent scopes.

(Some of this will get cleaned up with SR-2209.)

rdar://problem/27663492
2016-08-11 14:30:09 -07:00
Jordan Rose
b5003f4c48 Private members may not satisfy protocol requirements, ever. (#3842)
* Private members may not satisfy protocol requirements, ever.

...because by construction they can be invoked from outside of the
type.

Finishing up SE-0025 ('private' and 'fileprivate').

* Update docs and mark SE-0025 ('private' and 'fileprivate') as done!

There's still improvements we can make (see 508e825f), but the feature
is in place and should be working correctly.
2016-07-29 11:24:05 -07:00
David Farler
7bfaeb57f1 [SE-0081] Warn on deprecated where clause inside angle brackets
and provide a fix-it to move it to the new location as referenced
in SE-0081.

Fix up a few stray places in the standard library that is still using
the old syntax.

Update any ./test files that aren't expecting the new warning/fix-it
in -verify mode.

While investigating what I thought was a new crash due to this new
diagnostic, I discovered two sources of quite a few compiler crashers
related to unterminated generic parameter lists, where the right
angle bracket source location was getting unconditionally set to
the current token, even though it wasn't actually a '>'.
2016-07-26 01:41:10 -07:00
Jordan Rose
e9334665a8 'private' top-level operators are OK for 'fileprivate' conformances
This part of the code /isn't/ using access scopes yet, and probably
should be switched to that soon, but for now, just allow top-level
operators marked 'private' to satisfy a conformance for a type or
protocol marked 'fileprivate'.

More progress on SE-0025 ('private' and 'fileprivate')
2016-07-25 13:13:35 -07:00
Jordan Rose
508e825ff2 Split 'fileprivate' and 'private', but give them the same behavior.
'fileprivate' is considered a broader level of access than 'private',
but for now both of them are still available to the entire file. This
is intended as a migration aid.

One interesting fallout of the "access scope" model described in
758cf64 is that something declared 'private' at file scope is actually
treated as 'fileprivate' for diagnostic purposes. This is something
we can fix later, once the full model is in place. (It's not really
/wrong/ in that they have identical behavior, but diagnostics still
shouldn't refer to a type explicitly declared 'private' as
'fileprivate'.)

As a note, ValueDecl::getEffectiveAccess will always return 'FilePrivate'
rather than 'Private'; for purposes of optimization and code generation,
we should never try to distinguish these two cases.

This should have essentially no effect on code that's /not/ using
'fileprivate' other than altered diagnostics.

Progress on SE-0025 ('fileprivate' and 'private')
2016-07-25 13:13:35 -07:00
Jordan Rose
306eddab26 SE-0025: Allow public members inside internal types. (#3404)
(and any other member with higher access control than its enclosing type)

There's no effect, but it is now considered legal and the compiler will
no longer warn about it. This allows an API author to prototype their
API with proper access levels and still limit the top-level type.

If the new getEffectiveAccess computation turns out to be expensive, we
can cache the result.

Note that the compiler will still warn when putting a public member
inside an extension explicitly marked internal, because the extended
type could be public and then including a public member would be valid.
It is also still an error to put a public member inside a constrained
extension of an internal type, though I think this one is safe to
relax later.

Progress on SE-0025 ('private' and 'fileprivate')
2016-07-11 14:28:23 -07:00
Jordan Rose
aee92ff612 SE-0025: Parsing and basic completion for 'fileprivate'. (#3391)
Right now 'fileprivate' is parsed as an alias for 'private' (or
perhaps vice versa, since the semantics of 'private' haven't changed
yet). This allows us to migrate code to 'fileprivate' without waiting
for the full implementation.
2016-07-07 15:20:41 -07:00
Chris Lattner
2c81c8a114 add some parens to the testsuite, NFC. 2016-05-05 23:19:08 -07:00
Doug Gregor
38c1de69e4 Reinstate "[SR-511][Parse] Add 'associatedtype' keyword and fixit"
This reverts commit ce7b2bcf09, tweaking
a few validation tests appropriately (1 crasher fixed, two -verify
tests that needed updating).
2016-01-14 00:21:48 -08:00