Commit Graph

49 Commits

Author SHA1 Message Date
Anthony Latsis
fffa8c2f51 Diag: Abstract away some calls to DeclAttribute::getAttrName 2025-03-28 02:01:27 +00:00
Visckmart
e18bfaba52 [Parser] Improve diagnostics for access-level modifiers
Improve the diagnostics for situations where multiple access-level modifiers are used on the same declaration. Keep the original duplicate error message if the access levels are the same.
2023-10-26 19:02:35 -03:00
Alexis Laferrière
8b098ab487 [Tests] Remove the flag AccessLevelOnImport from all tests but one 2023-10-10 09:09:22 -07:00
stevenwong
3cabe08d29 [Parser] Make parser aware of the unexpected attributes 2023-04-21 23:38:57 +08:00
Ellie Shin
77b5c79cc0 Allow package(set) at the top level decl site
Treat `package` as a non-contextual keyword when applicable
Add tests
Resolves rdar://105978922, rdar://104931420
2023-03-16 17:38:18 -07:00
Ellie Shin
0fe0d6d221 Allow Swift as a package name
Update AccessScope::isChildOf
Add more tests for package access level
Resolves rdar://106728606
2023-03-14 17:17:14 -07:00
Alexis Laferrière
08895657cb [Parser] Intro parser support and flag for access level on imports 2023-02-25 09:47:19 -08:00
Slava Pestov
4e60aa150a RequirementMachine: Pass -requirement-machine-inferred-signatures=off in a handful of tests
- unify_superclass_types_[23].swift: I will fix these soon.
  rdar://90469643

- attr/accessibility_where_clause.swift: This one is dicey and should
  probably be banned. rdar://90469477
2022-03-18 00:43:26 -04:00
Jordan Rose
15056ed695 Remove mistaken early exit from access checking
Warnings should not result in early exits because we might end up
missing an error!
2019-01-28 18:26:43 -08:00
Jordan Rose
f1856b8f47 Honor !EnableAccessControl when checking protocol witnesses
Useful in the LLDB REPL, and also for what I'm about to do to the
integrated REPL.
2018-09-22 13:47:16 -07:00
Slava Pestov
cdadee8173 AST: Fix layering violations in DefaultAndMaxAccessLevelRequest
Our walk over the requirement interface types meant that
computing the access level of an extension member depended
on type resolution and the GSB.

Fix this by adding a new request that simply collects all
TypeDecls referenced from a TypeRepr, and compute the
extension's maximum access level using that.

If we use Structural rather than Interface type resolution when
walking the extension's requirements, we don't have to build its
generic signature first.
2018-09-05 16:18:54 -07:00
naru-jpn
b44e7d1a10 Add indents 2018-06-13 18:09:59 +09:00
naru-jpn
1e1c3fe033 Add tests to fix wrong fixit when the type doesn't conform to a public protocol 2018-06-13 18:04:24 +09: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
Jeffrey MACKO
716457c135 Fix SR-5615 2017-12-16 01:23:36 -05:00
Jacob Bandes-Storch
32635696bd [Diagnostics] handle ‘open’ access control; introduce %error modifier 2017-01-06 22:48:29 -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
John McCall
c8c41b385c Implement SE-0077: precedence group declarations.
What I've implemented here deviates from the current proposal text
in the following ways:

- I had to introduce a FunctionArrowPrecedence to capture the parsing
  of -> in expression contexts.

- I found it convenient to continue to model the assignment property
  explicitly.

- The comparison and casting operators have historically been
  non-associative; I have chosen to preserve that, since I don't
  think this proposal intended to change it.

- This uses the precedence group names and higherThan/lowerThan
  as agreed in discussion.
2016-07-26 14:04:57 -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
758cf64283 Rework access checking in terms of "access scopes".
(in preparation for the private/fileprivate split)

An "access scope" is the outermost DeclContext where a particular
declaration may be referenced: for a 'fileprivate' declaration it's
the enclosing file, and for an 'internal' declaration it's the module.
'public' corresponds to a scope of "everything", represented by a null
DeclContext.

This model extends naturally to the (not-yet-implemented) SE-0025
notion of 'private', where the access scope is a declaration's
immediately enclosing DeclContext.

Complicating this model is the revised rules that allow, e.g., a public
declaration to be declared within an internal type. The access scope
for this declaration is still just the module, not "everything".

This commit reworks formal access control checking in terms of this
model, including tightening up some of the handling for '@testable'.
This implements the rule that you must be able to access a declaration's
type everywhere you can reference the declaration.

This was not intended to change compiler behavior, but in practice it
has made cross-file dependency tracking a bit more conservative
(unnecessarily), caught a mistake in diagnosing access violations,
and fixed a fuzzer-based crasher (see test changes).

Progress on SE-0025 ('private' and 'fileprivate')
2016-07-21 14:54:48 -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
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
Erik Eckstein
ce7b2bcf09 Revert "[SR-511][Parse] Add 'associatedtype' keyword and fixit"
This reverts commit 2b2e9dc80e.

It broke some compiler crasher tests
2016-01-13 20:42:58 -08:00
gregomni
2b2e9dc80e [SR-511][Parse] Add 'associatedtype' keyword and fixit
Adds an associatedtype keyword to the parser tokens, and accepts either
typealias or associatedtype to create an AssociatedTypeDecl, warning
that the former is deprecated. The ASTPrinter now emits associatedtype
for AssociatedTypeDecls.

Separated AssociatedType from TypeAlias as two different kinds of
CodeCompletionDeclKinds. This part probably doesn’t turn out to be
absolutely necessary currently, but it is nice cleanup from formerly
specifically glomming the two together.

And then many, many changes to tests. The actual new tests for the fixits
is at the end of Generics/associated_types.swift.
2016-01-13 17:54:31 -08:00
Doug Gregor
7ba3cd27a9 Stop setting a bound type on top-level IdentTypeReprs.
Swift SVN r32432
2015-10-04 06:18:23 +00:00
Chris Lattner
72c5c3e4fe Two changes:
- Enhance the branch new argument label overload diagnostic to just
   print the argument labels that are the problem, instead of printing
   the types inferred at the argument context.  This can lead to confusion
   particularly when an argument label is missing.  For example before:

error: argument labels '(Int)' do not match any available overloads
note: overloads for 'TestOverloadSets.init' exist with these partially matching parameter lists: (a: Z0), (value: Int), (value: Double)

after:

error: argument labels '(_:)' do not match any available overloads
note: overloads for 'TestOverloadSets.init' exist with these partially matching parameter lists: (a: Z0), (value: Int), (value: Double)


Second, fix <rdar://problem/22451001> QoI: incorrect diagnostic when argument to print has the wrong type
by specifically diagnosing the problem when you pass in an argument to a nullary function.  Before:

error: cannot convert value of type 'Int' to expected argument type '()'

after:
error: argument passed to call that takes no arguments
print(r22451001(5))
                ^




Swift SVN r31795
2015-09-09 00:26:37 +00:00
Chris Lattner
fc64acb4ae Add fixit checks to various test/attr tests.
Swift SVN r31002
2015-08-04 20:29:00 +00:00
Chris Lattner
97e6a50148 Start using contextual information from function calls to diagnose issues in their
argument.  For now we start with some of the most simple cases: single argument 
calls.  This dramatically improves the QoI for error messages in argument lists,
typically turning a error+note combo into a single specific error message.

Some minor improvements coming (and also generalizing this to n-ary calls), but it 
is nice that all the infrastructure is starting to come together...



Swift SVN r30905
2015-08-01 04:37:52 +00:00
Chris Lattner
649e5f937d generalize the existing ? and + constraints on expected diagnostics to
a simpler and more general * constraint.  This paves the way for other improvements.



Swift SVN r30622
2015-07-25 05:23:30 +00:00
Jordan Rose
e77e0587c7 Enforce maximum allowed access for extensions with 'where' clauses.
Otherwise, we end up with declarations with public access that do not
have public 'self' types. These declarations can then be used by other
modules, which may end up trying to access non-external symbols.

This closes a loophole currently in use by the standard library, so
the '_prext_ReverseIndexType' and '_ReverseCollectionType' protocols
become public for now. In order to keep the API impact minimized,
extensions involving these protocols now extend them directly, so that
all of the "private" stuff shows up in one place in the generated
interface. This is not a long-term solution, but it's no worse than
the rest of the underscore rules in the standard library.

rdar://problem/21380336 tracks relaxing access restrictions for protocol
conformances when the witnesses come from a different type, like a
protocol extension. This requires some SILGen work to do correctly.

Finishes rdar://problem/21559986

Swift SVN r30612
2015-07-25 01:06:50 +00:00
Jordan Rose
b0debdc696 Take requirements into account when checking an extension's explicit access.
This still doesn't check an extension's /members/, but does ensure that
there's not an extension declared public that clearly has non-public
types in its 'where' clause.

More rdar://problem/21559986

Swift SVN r30611
2015-07-25 01:06:46 +00:00
Chris Lattner
63f99a486c Move CallExpr diagnostics over to the same overload candidate diagnosis
facilities used by operators etc.  This required a bunch of changes to make
the diagnostics changes strictly an improvement:

  - Teach the new path about calls to TypeExprs.
  - Teach evaluateCloseness some simple things about varargs.
  - Make the generic diagnosis logic produce a better error when there is 
    exactly one match.

Overall, the resultant diagnostics are a step forward: we now produce candidate
set notes more uniformly, and the messages about some existing ones are 
more specific.  This is just another stepping stone towards progress though.



Swift SVN r30057
2015-07-10 04:26:42 +00:00
Doug Gregor
b2cc34c241 Remove '#' for making parameter names into argument labels.
If you want to make the parameter and argument label the same in
places where you don't get the argument label for free (i.e., the
first parameter of a function or a parameter of a subscript),
double-up the identifier:

  func translate(dx dx: Int, dy: Int) { }

Make this a warning with Fix-Its to ease migration. Part of
rdar://problem/17218256.

Swift SVN r27715
2015-04-24 23:58:57 +00:00
Doug Gregor
9271a24a92 Introduce a protocol conformance registry for nominal types.
(Note that this registry isn't fully enabled yet; it's built so that
we can test it, but has not yet taken over the primary task of
managing conformances from the existing system).

The conformance registry tracks all of the protocols to which a
particular nominal type conforms, including those for which
conformance was explicitly specified, implied by other explicit
conformances, inherited from a superclass, or synthesized by the
implementation.

The conformance registry is a lazily-built data structure designed for
multi-file support (which has been a problematic area for protocol
conformances). It allows one to query for the conformances of a type
to a particular protocol, enumerate all protocols to which a type
conforms, and enumerate all of the conformances that are associated
with a particular declaration context (important to eliminate
duplicated witness tables).

The conformance registry diagnoses conflicts and ambiguities among
different conformances of the same type to the same protocol. There
are three common cases where we'll see a diagnostic:

1) Redundant explicit conformance of a type to a protocol:

    protocol P { }
    struct X : P {  }
    extension X : P { } // error: redundant explicit conformance

2) Explicit conformance to a protocol that collides with an inherited
  conformance:

    protocol P { }
    class Super : P { }
    class Sub : Super, P { } // error: redundant explicit conformance

3) Ambiguous placement of an implied conformance:

    protocol P1 { }
    protocol P2 : P1 { }
    protocol P3 : P1 { }

    struct Y { }
    extension Y : P2 { }
    extension Y : P3 { } // error: ambiguous implied conformance to 'P1'

  This happens when two different explicit conformances (here, P2 and
  P3) placed on different declarations (e.g., two extensions, or the
  original definition and other extension) both imply the same
  conformance (P1), and neither of the explicit conformances imply
  each other. We require the user to explicitly specify the ambiguous
  conformance to break the ambiguity and associate the witness table
  with a specific context.

Swift SVN r26067
2015-03-12 21:11:23 +00:00
Dmitri Hrybenko
3b04d1b013 tests: reorganize tests so that they actually use the target platform
Most tests were using %swift or similar substitutions, which did not
include the target triple and SDK.  The driver was defaulting to the
host OS.  Thus, we could not run the tests when the standard library was
not built for OS X.

Swift SVN r24504
2015-01-19 06:52:49 +00:00
Jordan Rose
3a5bfd6de0 Re-add invalid parse tests for the accessibility keywords.
Now that these are no longer context-sensitive, we don't have to worry about
alternate interpretations.

Swift SVN r20028
2014-07-16 18:04:13 +00:00
Jordan Rose
759b7b719d Add a test that 'private' is rejected on imports and operators.
This was fixed by Chris's "actually validate attribute subjects" change,
but it means we can close out <rdar://problem/17674038>.

Swift SVN r20027
2014-07-16 18:04:10 +00:00
Chris Lattner
6f3fe21d01 improve a diagnostic to talk about the "override modifier" instead of "override attribute"
Swift SVN r20005
2014-07-16 04:48:20 +00:00
Chris Lattner
ea8b7a471a Extend the attributes machinery to enforce the "OnFoo" flags in Attr.def at sema time, and extend
them to cover all declaration types.

This ensures that we reject attributes on declkinds where they don't make sense.  I went so far
as to make the QoI decent when an attribute can only be applied to a single kind of declaration
to make sure the error message says "@IBAction is only valid on 'func' declarations" as well.

This resolves <rdar://problem/17681151> 'dynamic' accepted by the compiler where it shouldn't be




Swift SVN r19982
2014-07-15 20:30:21 +00:00
Jordan Rose
10815eb5f9 s/attribute/modifier/ in a number of diagnostics.
Swift SVN r19972
2014-07-15 04:57:04 +00:00
Jordan Rose
40fcb27f9b [Accessibility] Check the subject first for setter accessibility attributes.
Swift SVN r19752
2014-07-09 22:26:00 +00:00
Jordan Rose
ee6d4bfde3 [Accessibility] Extensions with accessibility can't conform to protocols.
It's just confusing, since the protocol conformance is not bound by the
access level specified by the user.

Swift SVN r19751
2014-07-09 22:25:59 +00:00
Jordan Rose
1ee03760e4 Add a test for "public(set)" on a property in a private extension.
No functionality change.

Swift SVN r19749
2014-07-09 22:10:22 +00:00
Jordan Rose
ac90133b8c Update tests for accessibility modifiers becoming context-sensitive keywords.
Swift SVN r19673
2014-07-08 02:17:49 +00:00
Jordan Rose
40e9cd8e43 Check 'public(set)' modifiers a little more.
- Setters cannot (in this release) have more accessibility than getters.
- The modifiers don't make sense for constants and read-only vars/subscripts.

Swift SVN r19548
2014-07-04 01:53:48 +00:00
Jordan Rose
f6912b431b Validate which decls can have accessibility attributes.
Swift SVN r19141
2014-06-24 21:32:06 +00:00
Jordan Rose
deaa0a1497 Parse @private, @internal, @public, and the (set) variants of each.
This does no validation of the access control modifiers.

As part of this commit, note that "virtual" attributes may actually be
written in the source under another spelling. Update a few other parts of
the source to account for that.

Swift SVN r19140
2014-06-24 21:32:03 +00:00