Skip to content

[interop] clang name lookup should find declarations in inline namesp… #40127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2021

Conversation

hyp
Copy link
Contributor

@hyp hyp commented Nov 10, 2021

…aces

@hyp hyp requested review from guitard0g and zoecarver November 10, 2021 20:54
@hyp
Copy link
Contributor Author

hyp commented Nov 10, 2021

@swift-ci please test

@zoecarver zoecarver added the c++ interop Feature: Interoperability with C++ label Nov 10, 2021
Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions for tests to add:

  • Extending an inline namespace in Swift.
  • A type with the same name in two inline namespaces (this might be an ODR violation...).
  • A function inside of an inline namespace.
  • A test for std.inner and a test for std.__1.__2.inner.
  • If it's allowed, an inline namespace that is at the top level.
  • (Optional) a namespace inside of an inline namespace inside of a namespace.

// RUN: %target-swift-emit-ir -I %t/Inputs -enable-cxx-interop %t/test.swift | %FileCheck %t/test.swift


//--- Inputs/module.modulemap
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this new way to write tests :)


//--- Inputs/module.modulemap
module std {
header "string.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string.h is a header that may already be in our search paths, so maybe another name is better.

Comment on lines 14 to 19
template<class CharT>
struct basic_string;

typedef basic_string<char> string;

template<class CharT>
struct basic_string {
void *p;

const CharT *c_str();
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem like a really necessary part of the test. It's sort of complicated. It might be better to just use simple types/functions.

const CharT *c_str();
};

struct other {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally we use the TitleCase format for types. I know this is a test... but might as well start off right.


}

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you mark these as the end of the namespaces they close?

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 595871fa1efab118c92b231c76f235a1ebbe1d12

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 595871fa1efab118c92b231c76f235a1ebbe1d12

@hyp hyp force-pushed the eng/cxx-inline-ns-lookup branch from 595871f to 1ede3ef Compare November 11, 2021 20:20
@hyp
Copy link
Contributor Author

hyp commented Nov 11, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 1ede3efde3b8a4f0db5bbdde935b26194067ed37

});
llvm::copy_if(foundDecls, std::back_inserter(filteredDecls),
[clangDecl](SwiftLookupTable::SingleEntry decl) {
auto foundClangDecl = decl.dyn_cast<clang::NamedDecl *>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you run into problems with this? Macros can't be members so this should be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this broke the stdlib build.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's super interesting. Do you know how it broke the build or what decl it was? I might go investigate after this lands..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was p_starttime

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which I believe is a macro

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's super interesting... so it's a macro defined to a member of a struct. I guess this is the correct way to handle this...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish this could just be a ranges::filter(isa-named-decl) | ranges::filter(...)

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for overhauling the test. They look great now!

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 1ede3efde3b8a4f0db5bbdde935b26194067ed37

@hyp hyp force-pushed the eng/cxx-inline-ns-lookup branch from 1ede3ef to 34656fd Compare November 12, 2021 21:20
@hyp
Copy link
Contributor Author

hyp commented Nov 12, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 34656fdab9408bb0a1e1489dedd7c30751517d5d

@hyp hyp force-pushed the eng/cxx-inline-ns-lookup branch from 34656fd to 71e9119 Compare November 12, 2021 23:17
@hyp
Copy link
Contributor Author

hyp commented Nov 12, 2021

@swift-ci please test

// declarations due to inline namespaces. We still merge in the other
// entries found in the lookup table, to support finding members in
// namespace extensions.
if (!allFound.empty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to duplicate the code anyway, might as well unconditionally take this path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code isn't duplicated, the regular path does something additional - it marks the name as markLazilyComplete in the table.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so, if we know that there are no lazy members (i.e., all members are in the lookup table for a given name), then we don't have to emit a namespace lookup request? Makes sense.

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks for the diligent updates, Alex!

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 71e9119c7fe495ccf184abfc90b115e01ead931c

@hyp hyp force-pushed the eng/cxx-inline-ns-lookup branch from 71e9119 to d570826 Compare November 13, 2021 01:35
@hyp
Copy link
Contributor Author

hyp commented Nov 13, 2021

@swift-ci please test

@hyp hyp force-pushed the eng/cxx-inline-ns-lookup branch from d570826 to 1089959 Compare November 13, 2021 01:40
@hyp
Copy link
Contributor Author

hyp commented Nov 13, 2021

@swift-ci please test

@hyp
Copy link
Contributor Author

hyp commented Nov 13, 2021

@swift-ci please test source compatibility

@zoecarver
Copy link
Contributor

@hyp I don't think a source compatibility check is really necessary here. It seems very unlikely this will break source compatibility as we have few projects that use Objective-C interop and none that use C++ interop.

I think the failures you're seeing for source compatibility are failing on main and will be resolved by #40143. Anyway, I think you're good to merge if you want.

@hyp
Copy link
Contributor Author

hyp commented Nov 13, 2021

@hyp I don't think a source compatibility check is really necessary here. It seems very unlikely this will break source compatibility as we have few projects that use Objective-C interop and none that use C++ interop.

I think the failures you're seeing for source compatibility are failing on main and will be resolved by #40143. Anyway, I think you're good to merge if you want.

I know, I saw that the same failures are in the baseline :) I was just curious to see what the source compatibility test would do.

@hyp hyp merged commit 04f6062 into swiftlang:main Nov 13, 2021
@zoecarver
Copy link
Contributor

I know I just approved this patch, but I also just had a thought: this will crash if someone tries to call a function in an inline namespace from the parent namespace, right? If so, that's really not a great failure mode on our part. Ideally we'd have a diagnostic, but it probably doesn't make sense to spend time adding that, as we want to fix this issue pretty soon. How hard would it be to filter out FuncDecls when filter on the inline parent? Maybe one of us can make a patch to do this next week (or maybe it's not needed depending on how quickly we plan to fix the underlying issue).

@hyp
Copy link
Contributor Author

hyp commented Nov 13, 2021

If so, that's really not a great failure mode on our part. Ideally we'd have a diagnostic, but it probably doesn't make sense to spen

It produces a diagnostic , unless I'm misunderstanding your scenario. One of the test cases covers it.

@zoecarver
Copy link
Contributor

It produces a diagnostic , unless I'm misunderstanding your scenario. One of the test cases covers it.

Ah, no, you're right. It doesn't crash. I was mis remembering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants